PHP:将日期转换为datetime并将其插入到mysql数据库中?

I have an input field in my html page that gets Date from jQuery datepicker.

The format looks like this:

18/02/2017

I have a MYSQL field which is DATETIME

I need to insert the Date above into this MYSQL field.

So i tried this code:

$mysqlDate = date('Y-m-d H:i:s', strtotime($_POST['php_date']));

but the result of $mysqlDate is this:

1970-01-01 01:00:00

Could someone please advise on this issue?

Thanks in advance.

Do one thing,

$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date'])));
echo $mysqlDate;

Give it a try,

it should work.

Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

Source link.

Your new concern answer,

$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date']).' + '.rand(30,60*60*24*3).' seconds'));

You have to convert the posted date to a

YYYY-MM-DD

Format before converting it into date time.

You can do

$date=$_POST['php_date'];
list($day,$month,$year)=explode('/', $date);
$date=$year.'-'.$month.'-'.$day;

And then use $date.