php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75468 Adding +1 month using DateTime, it returns incorrect DateTime.
Submitted: 2017-10-31 09:48 UTC Modified: 2017-10-31 18:32 UTC
From: info at milinka dot co dot uk Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: info at milinka dot co dot uk
New email:
PHP Version: OS:

 

 [2017-10-31 09:48 UTC] 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"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-31 10:29 UTC] 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 10:38 UTC] salathe@php.net
-Status: Open +Status: Duplicate
 [2017-10-31 10:38 UTC] 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 18:32 UTC] rasmus@php.net
-Status: Duplicate +Status: Not a bug
 [2017-10-31 18:32 UTC] rasmus@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC