strtotime returning false date

荒凉一梦 提交于 2020-01-24 23:32:09

问题


I have a problem with strtotime function in php, i'm trying to convert mm-dd-yyyy to yyyy-mm-dd. The date is entered on the text box and on submit it should save the date to the database. the problem is it returns a wrong date(1970-01-01) each time,which means my code isn't taking the variable i use to store the date, My code is:

//dateconvert
$submitdate = date($_POST['date']);
$date   = date("Y-m-d", strtotime($submitdate));

//storeindb
$query ="INSERT INTO ticket SET date = '$date'";
$result = mysql_query($query);

I'm a newbie,please help.


回答1:


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.

http://php.net/manual/en/function.strtotime.php

You need to use the forward slash separator for mm/dd/yyyy formats.




回答2:


Use the code as:

$submitdate = $_POST['date'];
$date = date("Y-m-d", strtotime($submitdate));

Hope this hepls.




回答3:


date format in $submitdate is incorrect Because the format yyyy/mm/dd should be yyyy-mm-dd so you will need to replace your / characters.

Try this:

$submitdate = str_replace("/","-",$_POST['date']);
echo date('Y-m-d', strtotime($submitdate));


来源:https://stackoverflow.com/questions/19587113/strtotime-returning-false-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!