I'm working with a date format that may not be standard because strtotime is not parsing it correctly. The format is Sun Nov 16 10:10:10 GMT 2010. I'm trying to convert it to 2010-11-16 using date('Y-m-d', strtotime($date) but not working. Is there a way I could make modifications to the original date string so that it's close to a standard format so strtotime would accept it?

有帮助吗?

解决方案

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

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

EDIT

I believe you will achieve what you are looking for with the dateTime class
http://www.php.net/manual/en/class.datetime.php

EDIT 2

As @Mark Baker commented, the most correct function to use is DateTime::createFromFormat as you can see in this link:
http://www.php.net/manual/en/datetime.createfromformat.php

其他提示

strtotime() does know how to parse your format Sun Nov 16 10:10:10 GMT 2010 and it is parsed correctly. demo

Problem with your example is that date 2010-11-16 is not Sunday, but it is Tuesday, thats why you get first Sunday from date 2010-11-16 which is 2010-11-21. demo

DateTime::createFromFormat will return same datetime as strtotime(). demo

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top