更改日期格式-年份不正确


Changing date format - incorrect year

代码:

$expiration_date = "24th Dec, 2012";
var_dump( date( 'Y-m-d', strtotime( $expiration_date )));;
输出:

string(10) "2011-12-24"

为什么PHP不能正确解析年份(显示2011而不是2012)?

strtotime()不支持日期排序(参见2表)。使用DateTime类代替:

$date = DateTime::createFromFormat("jS M, Y", "24th Dec, 2012");
var_dump($date->format('Y-m-d')); // string(10) "2012-12-24"

可能与$expiration_date的格式有关。试着把月份放在日期的前面。

$expiration_date = "Dec 24th, 2012";

strtotime函数的输入必须是strtotime可以理解的格式