php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72291 DateTime miss February 2017
Submitted: 2016-05-30 10:44 UTC Modified: 2016-05-30 11:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: gwartur at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.6.22 OS: Windows NT 6.1 build 7601
Private report: No CVE-ID: None
 [2016-05-30 10:44 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-30 10:47 UTC] gwartur at gmail dot com
Sorry. I'm confused Expected result and Actual result
 [2016-05-30 10:54 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-05-30 10:54 UTC] 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 11:13 UTC] gwartur at gmail dot com
Wow o_O. shame how...
Thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 06:01:29 2024 UTC