PHP strtotime 返回错误的时间戳


php strtotime return wrong timestamp

我正在使用php strtotime函数(日期格式Day, month and two digit year, with dots or tabs D.M.Y,来自文档:http://www.php.net/manual/en/datetime.formats.date.php)并发现问题:

从 php.net 示例 strtotime("30.6.08") -> 1214773200 -> 周日, 29 六月 2008 21:00:00 GMT(反向转换右)

另一个变体 strtotime("24.10.13") -> 1381180213 -> 周一, 07 Oct 2013 21:10:13 GMT (反向不正确)

但是 strtotime("30.10.13") -> 1383084000 -> 2013 年 10 月 29 日星期二 22:00:00 GMT (再次正确反向结果)

怎么了?

Strtotime正在猜测您以什么格式发送日期并猜测错误。您应该使用DateTime(),您可以在其中指定发送日期的格式。

$date = DateTime::createFromFormat('d.m.y', $datestring);
echo $date->format('Y-m-d');