当使用“第一天”修改日期时,PHP中有一种非常奇怪的行为。
'first day' ' of'?设置本月第一天。(http://php.net/manual/de/datetime.formats.relative.php)
$currentMonthDate = new DateTime("2014-07-30 10:26:00.000000");
echo $currentMonthDate->format('Ym');
$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');201407 / 201407 OK
$currentMonthDate = new DateTime("2014-07-31 10:26:00.000000");
echo $currentMonthDate->format('Ym');
$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');201407 / 201408为什么?
$currentMonthDate = new DateTime("2014-08-01 10:26:00.000000");
echo $currentMonthDate->format('Ym');
$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');201408 / 201408 OK
我在运行PHP5.2的生产服务器上发现了这种行为,所以我假设它是一个古老的bug,但它发生在PHP5.3 (http://phptester.net/)和我们的测试服务器上的5.5中。
如果我在PHP5.2中使用“这个月的第一天”,也会发生同样的行为。在PHP5.5中,“本月的第一天”按预期工作。
“第一天”-behaviour是一个bug吗?以及如何在PHP5.2中获得“本月的第一天”,而无需在string和date之间进行奇怪的转换
发布于 2014-07-31 12:25:45
这个问题似乎是一个文档问题。
来自bug #51096中的Derick:“第一天”和“最后一天”应该是"+1天“和"-1天”。 文档应该更新以反映这种行为,因为当前的“第一/最后一天”部分说"of“是可选的,而不是。
更新
文档现在是固定的。因此,' of'?不再是可选的了。
'first day of'设置本月第一天。
https://stackoverflow.com/questions/25054592
复制相似问题