1

I use ajax procedure for passing variables:

var a = 01;
var b = 2;
var c = 2016;

On php side I need to create this date - 01-Feb-2016 - and update a datetime column in mysql database. Here is an example of that column conntent - 2016-12-09 00:00:00

target.php

extract($_POST);
$date=date_create($c . '-' . $b . '-' . $a);
echo date_format($date, "Y-M-d"); // first try
$date = date("Y-M-d", $date); // second try
echo $date;

In both cases I'm getting errors.
Any help?

4
  • 1
    I can assume, but what errors are you getting? Commented Jan 25, 2017 at 22:14
  • 3
    Please do not use extract($_POST). That is extremely dangerous as you're allowing anyone who can submit the form to create variables in your code. Commented Jan 25, 2017 at 22:14
  • Assuming you are getting some data. In both cases you have not read the manual page for the function you are guessing might be useful to you. Commented Jan 25, 2017 at 22:19
  • 2
    Use mktime() to create a PHP date directly from numeric input, instead of parsing a string. Commented Jan 25, 2017 at 22:35

1 Answer 1

2

You're probably looking for date and strtotime functions to be used in conjunction:

$a = $_POST['a'];         // assumed: '01'
$b = $_POST['b'];         // assumed: '2'
$c = $_POST['c'];         // assumed: '2016'

echo "<br>(1) yyyy-mm-dd        :   ".date("Y-m-d", strtotime($a."-".$b."-".$c));
echo "<br>(2) dd-MMM-yyyy       :   ".date("d-M-Y", strtotime($a."-".$b."-".$c));
echo "<br>(3) MySQL Timestamp   :   ".date("Y-m-d H:i:s", strtotime($a."-".$b."-".$c));


Which will generate the output as:

(1) yyyy-mm-dd      :   2016-02-01
(2) dd-MMM-yyyy     :   01-Feb-2016
(3) MySQL Timestamp :   2016-02-01 00:00:00


Note that H will give you hours in 24 hour format and h in 12 hour format which will force convert the time fragment in the string (if any) to a 12 hour format, ignoring the AM or PM bit - unless the format used was Y-m-d h:i:s A.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.