php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67663 DatePeriod not giving correct date
Submitted: 2014-07-21 20:52 UTC Modified: 2014-07-22 08:14 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: hharrysidhu at gmail dot com Assigned:
Status: Wont fix Package: Date/time related
PHP Version: 5.5.14 OS: Windows
Private report: No CVE-ID: None
 [2014-07-21 20:52 UTC] hharrysidhu at gmail dot com
Description:
------------
$begin = new DateTime( '2014-12-31' );
$end = new DateTime( '2016-04-31');

$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

This code should give me 31 of every month by skipping February.
It skips February but it changes actual date. 

Test script:
---------------
$begin = new DateTime( '2014-12-31' );
$end = new DateTime( '2015-06-31');

$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

Expected result:
----------------
2014-12-31
2015-01-31
2015-03-31
2015-05-31

Actual result:
--------------
2014-12-31
2015-01-31
2015-03-03
2015-04-03
2015-05-03
2015-06-03

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-22 02:52 UTC] chris at cgsmith dot net
By doing format("U") I found that P1M equals 2,678,400 seconds.  It looks like 'P1M' is literally translated into '31 days'
 [2014-07-22 08:14 UTC] salathe@php.net
-Status: Open +Status: Wont fix -Package: *General Issues +Package: Date/time related
 [2014-07-22 08:14 UTC] salathe@php.net
The "P1M" interval does the only sensible thing it can; it increments the month number only.  "2015-01-31" + "P1M" gives "2015-02-31", which is not a valid date so the normal "roll over" behaviour occurs to give the resulting date of "2015-03-03".

The feature request made here, to "give [you] 31 of every month by skipping February" seems like a very strange determination of "correct" behaviour and will not be something that we support.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC