Bug #72291 [Nab]: DateTime miss February 2017

From: Date: Mon, 30 May 2016 11:13:01 +0000
Subject: Bug #72291 [Nab]: DateTime miss February 2017
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201342@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72291&edit=1

 ID:                 72291
 User updated by:    gwartur at gmail dot com
 Reported by:        gwartur at gmail dot com
 Summary:            DateTime miss February 2017
 Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 Operating System:   Windows NT 6.1 build 7601
 PHP Version:        5.6.22
 Block user comment: N
 Private report:     N

 New Comment:

Wow o_O. shame how...
Thanks!


Previous Comments:
------------------------------------------------------------------------
[2016-05-30 10:54:42] requinix@php.net

Format the date with the real day number and not just 01 and you can see what's happening. https://3v4l.org/3YJ25

2017-01-30 + P1M = 2017-02-30, which overflows two days into March = 2017-03-02

If you want to use that method for incrementing months then make sure you start on a day that every
month will have. https://3v4l.org/moWot

------------------------------------------------------------------------
[2016-05-30 10:47:58] gwartur at gmail dot com

Sorry. I'm confused Expected result and Actual result

------------------------------------------------------------------------
[2016-05-30 10:44:06] gwartur at gmail dot com

Description:
------------
I use an object {DateTime} for mass generation intervals in the database. When generating discovered
a serious error. By moving the dates in 1 month I have not received February 2017

Test script:
---------------
$lastDate = new DateTime();
$lastDate->sub(new DateInterval("P1M"));

$generateToThisDate = new DateTime();
$generateToThisDate->add(new DateInterval("P20M"));

while ($lastDate < $generateToThisDate) {
  $lastDate->add(new DateInterval("P1M"));
  echo $lastDate->format('Y-m-01') . " - " .
$lastDate->format('Y-m-t') . "\n";
}


Expected result:
----------------
list of new dates:
2016-05-01 - 2016-05-31
2016-06-01 - 2016-06-30
2016-07-01 - 2016-07-31
2016-08-01 - 2016-08-31
2016-09-01 - 2016-09-30
2016-10-01 - 2016-10-31
2016-11-01 - 2016-11-30
2016-12-01 - 2016-12-31
2017-01-01 - 2017-01-31
2017-03-01 - 2017-03-31
2017-04-01 - 2017-04-30
2017-05-01 - 2017-05-31
2017-06-01 - 2017-06-30
2017-07-01 - 2017-07-31
2017-08-01 - 2017-08-31
2017-09-01 - 2017-09-30
2017-10-01 - 2017-10-31
2017-11-01 - 2017-11-30
2017-12-01 - 2017-12-31
2018-01-01 - 2018-01-31
2018-02-01 - 2018-02-28
2018-03-01 - 2018-03-31
2018-04-01 - 2018-04-30
2018-05-01 - 2018-05-31
2018-06-01 - 2018-06-30

Actual result:
--------------
list of new dates:
2016-05-01 - 2016-05-31
2016-06-01 - 2016-06-30
2016-07-01 - 2016-07-31
2016-08-01 - 2016-08-31
2016-09-01 - 2016-09-30
2016-10-01 - 2016-10-31
2016-11-01 - 2016-11-30
2016-12-01 - 2016-12-31
2017-01-01 - 2017-01-31
2017-02-01 - 2017-02-28
2017-03-01 - 2017-03-31
2017-04-01 - 2017-04-30
2017-05-01 - 2017-05-31
2017-06-01 - 2017-06-30
2017-07-01 - 2017-07-31
2017-08-01 - 2017-08-31
2017-09-01 - 2017-09-30
2017-10-01 - 2017-10-31
2017-11-01 - 2017-11-30
2017-12-01 - 2017-12-31
2018-01-01 - 2018-01-31
2018-02-01 - 2018-02-28
2018-03-01 - 2018-03-31
2018-04-01 - 2018-04-30
2018-05-01 - 2018-05-31
2018-06-01 - 2018-06-30


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=72291&edit=1


Thread (4 messages)

« previous php.bugs (#201342) next »