Bug #75468 [Dup->Nab]: Adding +1 month using DateTime, it returns incorrect DateTime.
| From: | rasmus@php.net | Date: | Tue, 31 Oct 2017 18:32:21 +0000 |
| Subject: | Bug #75468 [Dup->Nab]: Adding +1 month using DateTime, it returns incorrect DateTime. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-212391@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75468&edit=1
ID: 75468
Updated by: rasmus@php.net
Reported by: info at milinka dot co dot uk
Summary: Adding +1 month using DateTime, it returns incorrect
DateTime.
-Status: Duplicate
+Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: any
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
This isn't a bug. This is how UNIX date math works. Try it from your command line (on Oct.31):
$ date --date='+1 month' +'Next month is %B?'
Next month is December?
That's why PHP supports "first day of next month". Use that instead if you just want
the next month.
Previous Comments:
------------------------------------------------------------------------
[2017-10-31 10:38:49] salathe@php.net
Using "+1 month" just increments the month number (e.g. "2017-10-31 +1 month"
gives "2017-11-31") and when that result is not a valid date (e.g. there is no 31st of
November), the normal mechanism applies and amends the date to be a valid one (e.g. the 31st day of
November, is the 1st of December).
See duplicate bugs:
bug #75012
bug #74610
bug #73298
bug #72291
bug #70186
... to name a few.
------------------------------------------------------------------------
[2017-10-31 10:29:34] info at milinka dot co dot uk
$date = new DateTime();
$date->setDate(2017, 1, 31);
$date->modify("+1 months");
var_dump($date->format('d.m.Y'));
--> "2017-03-03 03:25:01.010311"
------------------------------------------------------------------------
[2017-10-31 09:48:40] info at milinka dot co dot uk
Description:
------------
NOTE: This bug can replicated only today. 31 Oct 2017.
When adding +1 month using DateTime, it returns incorrect DateTime.
Code:
$begin = new DateTime("now");
$end = clone $begin;
$end->modify("+1 month");
var_dump($begin,$end);
Outputs dates, where end date is wrong:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2017-10-31 02:41:11.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#2 (3) {
["date"]=>
string(26) "2017-12-01 02:41:11.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
Test script:
---------------
$begin = new DateTime("now");
$end = clone $begin;
$end->modify("+1 month");
var_dump($begin,$end);
Expected result:
----------------
object(DateTime)#1 (3) {
["date"]=>
string(26) "2017-10-31 02:41:11.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#2 (3) {
["date"]=>
string(26) "2017-11-30 02:41:11.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
Actual result:
--------------
object(DateTime)#1 (3) {
["date"]=>
string(26) "2017-10-31 02:41:11.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#2 (3) {
["date"]=>
string(26) "2017-12-01 02:41:11.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75468&edit=1