php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44073 strtotime "+1 Month" fails on 31st day of month
Submitted: 2008-02-07 20:28 UTC Modified: 2008-02-11 08:32 UTC
From: freedman at freeformit dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.5 OS: FC6
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: freedman at freeformit dot com
New email:
PHP Version: OS:

 

 [2008-02-07 20:28 UTC] freedman at freeformit dot com
Description:
------------
strtotime("+1 Month")

produces the wrong result on the 31st day of the month.

on January 31, date("m",strtotime("+1 Month")) will return 03 instead of 02

even if the documentation were to state that this is acceptable, it basically renders the function useless, since you'd have to always add a bunch of extra code to find out if it's the 31st so this really should be fixed.

basically "+1 Month" is NOT the same as "+30 Days"

I realize there's an issue when the 'day' of the month doesn't exist in the next month, however, this happens in half the months of the year, so basically, logically, I'd think the "last" day of this month "+1 month" would be the last day of the next month.  in cases where next month has fewer days than this month.

Using the example from http://bugs.php.net/bug.php?id=22486&edit=2
"rasmus@php.net 
If I told you on January 30 that I would come back in exactly one month to beat the crap out of you, when would you think I would show up?"

However, if I told you on January 1 at 8am that I would come back in exactly one month to beat the crap out of you, according to his presumption that a month is "30.5" days, then would you expect me to show up on February 1 or January 31.

The point is..  what "+1 Month" means, works differently on the 1st day of the month than it does on the last day of the month, and given the example of January 1 here, we can see that what it does on the 1st of a 31 day month and what it does on the last day preceeding a shorter month seem to apply different logic.


Reproduce code:
---------------
<?
$t=strtotime("January 1, 2008");
echo date("c\n", strtotime("+1 Month",$t));
$t=strtotime("January 29, 2008");
echo date("c\n", strtotime("+1 Month",$t));
$t=strtotime("January 31, 2008");
echo date("c\n", strtotime("+1 Month",$t));
$t=strtotime("February 29, 2008");
echo date("c\n", strtotime("+1 Month",$t));
?>

produces:
2008-02-01T00:00:00-08:00
2008-02-29T00:00:00-08:00
2008-03-02T00:00:00-08:00
2008-03-29T00:00:00-07:00


Expected result:
----------------
expected result:
2008-02-01T00:00:00-08:00
2008-02-29T00:00:00-08:00
2008-02-29T00:00:00-08:00
2008-03-29T00:00:00-08:00


Actual result:
--------------
2008-02-01T00:00:00-08:00
2008-02-29T00:00:00-08:00
2008-03-02T00:00:00-08:00
2008-03-29T00:00:00-07:00


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-11 08:32 UTC] derick@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

Many similar reports... and it's NOT a bug. From PHP 5.3 and higher you can do:

"first day of +1 month" or "first day of next month" or even "last day of next month" - those are always safe to use with just a "m" or another month date format specifier.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 17:01:31 2024 UTC