php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62984 "-1 month" in date moves day 31 to day 1 in the same month.
Submitted: 2012-08-31 16:32 UTC Modified: 2012-09-03 02:41 UTC
From: eyre dot jonathan at gmail dot com Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 5.3.16 OS: Debian Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
21 - 5 = ?
Subscribe to this entry?

 
 [2012-08-31 16:32 UTC] eyre dot jonathan at gmail dot com
Description:
------------
When looping backwards in time using the "-1 Month" feature of date(), July is printed twice. This actually occurs whenever $finalEndDate is a month with 31 days.

The "-1 Month" syntax does not work when the date is the 31st of the month. It apparently tries to set July 31 to June 31, which does not exist. The 31st of June is calculated to be July 1st, hence the double printing of July 2012 in the example below.

Test script:
---------------
$finalEndDate = "8/31/2012";
$finalStartDate = "12/31/2010";
$StartTime = strtotime($finalStartDate); //$StartDateDB);
$EndTime = strtotime($finalEndDate); //$EndDateDB);
$StartMark = getdate($StartTime);
$EndMark = getdate($EndTime);
$u = 0;
while ($StartTime <= $EndTime) {
    $MonthString = $EndMark["month"] . " " . $EndMark["year"];
    echo "comparing [" . $StartTime . "] to [" . $EndTime . "]... created[" . $MonthString . "]<BR>\n";
    $u++;
    $EndTime = strtotime("-1 month", $EndTime); // THIS IS THE BUGGY LINE
    $EndMark = getdate($EndTime);
}

Expected result:
----------------
comparing [1338440400] to [1346389200]... created[August 2012]
comparing [1338440400] to [1343710800]... created[July 2012]
comparing [1338440400] to [1338526800]... created[June 2012]

Actual result:
--------------
comparing [1338440400] to [1346389200]... created[August 2012]
comparing [1338440400] to [1343710800]... created[July 2012]
comparing [1338440400] to [1341118800]... created[July 2012]
comparing [1338440400] to [1338526800]... created[June 2012]

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-03 02:41 UTC] aharvey@php.net
Expected behaviour. Duplicate of bug #22486, bug #44073, bug #62980, and probably about fifty other bugs in the tracker.
 [2012-09-03 02:41 UTC] aharvey@php.net
-Status: Open +Status: Duplicate
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 22:01:29 2024 UTC