strtotime() does not return correct value when specifying date in dd/mm/yyyy format

浪尽此生 提交于 2019-12-12 18:06:21

问题


I want to convert date 24/09/2010 in format dd/mm/yyyy to 2010-09-24 in format yyyy-mm-dd.

This works:

date("Y-m-d",strtotime("09/24/2010"));

But this does not:

date("Y-m-d",strtotime("24/09/2010")); // it returns '1970-01-01'

Any idea why?


回答1:


according to php, the valid php formats are stated here. So basically what you gave is invalid.

Alternatively, you can use mktime, date_parse_from_format or date_create_from_format




回答2:


strtotime does its best to guess what you mean when given a string, but it can't handle all date formats. In you example, it is probably thinking that you are trying to refer to the 24th month, which isn't valid, and returns 0, which date then treats as the unix epoch (the date you got).

you can get around this using the mktime() and explode() functions, like so:

$date = "24/09/2010";
$dateArr = explode("/",$date);
$timeStamp = mktime(0,0,0,$dateArr[1],$dateArr[0],$dateArr[2]);
$newFormat = date("Y-m-d",$timeStamp);



回答3:


As you say, date("Y-m-d",strtotime("09/24/2010")) will work,because the date format--"09/24/2010"is correct, but "24/09/2010" is not the correct date format. you can find something useful here



来源:https://stackoverflow.com/questions/3809501/strtotime-does-not-return-correct-value-when-specifying-date-in-dd-mm-yyyy-for

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