第一天!="本月的第一天"

时间:2014-07-31 08:50:22

标签: php datetime

使用"第一天"在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 确定

$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 确定

我在运行PHP 5.2的生产服务器上发现了这种行为,所以我认为这是一个古老的错误,但它发生在我们的测试服务器上的PHP 5.3(http://phptester.net/)和5.5中。

如果我使用"本月的第一天"在PHP 5.2中出现相同的行为。在PHP 5.5"本月的第一天"按预期工作。

"第一天" -behaviour是一个错误?以及如何获得本月第一天的#34;在PHP 5.2中,没有在stringdate之间进行奇怪的转换?

1 个答案:

答案 0 :(得分:4)

似乎这个问题是文档问题。

  

来自Derick的bug#51096:       “第一天”和“最后一天”应为“+1天”和“-1天”。

     

应该更新文档以反映此行为   目前“第一天/最后一天”部分说“of”是可选的   当它不是。


更新

现在修复了文档。所以' of'?不再是可选的。

  

'first day of'设置当月第一天的日期。