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
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: eyre dot jonathan at gmail dot com
New email:
PHP Version: OS:

 

 [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: Tue Apr 16 09:01:28 2024 UTC