php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41547 strtotime bug
Submitted: 2007-05-31 09:21 UTC Modified: 2007-05-31 09:27 UTC
From: noisex at apollo dot lv Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.2 OS: FreeBSD 6.2
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: noisex at apollo dot lv
New email:
PHP Version: OS:

 

 [2007-05-31 09:21 UTC] noisex at apollo dot lv
Description:
------------
On months with 31th day the strtotime function with +/-$foo month calculates wrong. it calculates -30 days not 31 days.

Reproduce code:
---------------
Simple example (running today 31 May):

<?php
for($i=1; $i<=12; $i++) {
echo date("Y-M",strtotime("-$i months"))."<br>";
}
?>

Expected result:
----------------
2007-Mar
2007-Feb
2007-Jan
2006-Dec
2006-Nov
2006-Oct
2006-Sep
2006-Aug
2006-Jul
2006-Jun
2006-May
2006-Apr

Actual result:
--------------
2007-May
2007-Mar
2007-Mar
2007-Jan
2006-Dec
2006-Dec
2006-Oct
2006-Oct
2006-Aug
2006-Jul
2006-Jul
2006-May

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-31 09:25 UTC] noisex at apollo dot lv
The result must be (sorry): 
2007-Apr
2007-Mar
2007-Feb
2007-Jan
2006-Dec
2006-Nov
2006-Oct
2006-Sep
2006-Aug
2006-Jul
2006-Jun
2006-May
 [2007-05-31 09:27 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Your are incorrect in your assessment. The \"+-n months\" statement goes back one month. Back from May 31st that becomes April 31st. As April only has 30 days this turns into May 1st. The same is true February, which only has 28 days, so the result is March 3rd. See the following script:

<?php
for($i=1; $i<=12; $i++) {
    echo date(\"Y-M-d H:i:s\",strtotime(\"-$i months\")).\"<br/>\\n\";
}
?>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 10:01:31 2024 UTC