first day != "first day of this month"

There's a really strange behaviour in PHP when using "first day" to modify a date.

'first day' ' of'? Sets the day of the first of the current month. (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 WHY?

$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

I found this behaviour on our production-server running PHP 5.2, so I assumed it's a ancient bug, but it occurs in PHP 5.3 (http://phptester.net/) and 5.5 on our test-server.

If I use "first day of this month" in PHP 5.2 the same behaviour occurs. In PHP 5.5 "first day of this month" works as expected.

Is the "first day"-behaviour a bug? And how to get the "first day of this month" in PHP 5.2 without doing weird conversions between string and date ?


It seems like this issue is a Documentation Problem.

From Derick in bug #51096: "first day" and "last day" should be "+1 day" and "-1 day".

The documentation should be updated to reflect this behaviour, as currently the "first/last day of" section says the "of" is optional when it is not.


UPDATE

The documentation is now fixed. So the ' of'? is not optional anymore.

'first day of' Sets the day of the first of the current month.

链接地址: http://www.djcxy.com/p/80640.html

上一篇: transform3d():使用百分比在父对象内移动

下一篇: 第一天!=“本月的第一天”