Correct input values of strtotime

杀马特。学长 韩版系。学妹 提交于 2020-01-22 03:18:06

问题


I want to convert a date by the following statement

$date=date('d M Y, H:m',strtotime($date));

It printed out 09 Sep 2012, 11:09 when I tried various values of $date:

11:00 AM Sunday, 09 Sep 2012
Sunday 09 Sep 2012, 11:00 AM
09 Sep 2012 11:00 AM

What date format does the strtotime() function need? It seems to me at least one of them meets the "English textual datetime description" condition.


回答1:


Valid formats for strtotime are detailed here.




回答2:


From this site (http://www.tuxradar.com/practicalphp/4/5/2)

Be wary of dates such as this one: August 25, 2003, 10:26am. Although this may look perfectly well-formed, strtotime() is not able to handle it because it has commas in there - yes, they make it much more readable for us, but strtotime() gets confused handling them. If you have dates with commas in, be sure to strip them out using str_replace().

How to deal with strtotime() -> http://www.w3schools.com/php/func_date_strtotime.asp




回答3:


Looks like the only way to go is to sort the elements in proper order by regular expressions and then replace the month name by corresponding number. Such a string strtotime will manage. Note: I'm talking about PHP 5.2x

EDIT: I found the reason, why strtotime "wasn't working" was that I made a typo in the date formating string, there should be 'd M Y, H:i'



来源:https://stackoverflow.com/questions/12327028/correct-input-values-of-strtotime

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