php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43999 Date is not 'next month' as expected
Submitted: 2008-01-31 16:20 UTC Modified: 2009-08-06 22:43 UTC
From: gavinp at tbs dot uk dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 4.4.8 OS: Debian
Private report: No CVE-ID: None
 [2008-01-31 16:20 UTC] gavinp at tbs dot uk dot com
Description:
------------
Hi derick@php.net,

I have not posted this twice,(however because you locked the last bug I now have to and in case another user has ... your search system will need some work then. I could not find a bug describing the same problem. Please direct me to the correct place that is an open bug and I will happy tag this onto the end of it.

strtotime('next month', $basedate); where $basedate = today.

Should output the next month. The next month from today is Feb. Simple. That is the 'expected behaviour'.

That's like saying 2+2 = 4 except on Fridays when it = 5, and then saying because it's always been like this then it's 'Expected.'

If you are going to refer me to the documentation, please direct me to the documentations excact location where it says 'next month on the last day of the month should be two months instead of one.' I can not find this part in the documentation anywhere also.

However I would love to eat humble pie so please do show me.

Reproduce code:
---------------
$basedate = time(); 

$date1 = strtotime('next month', $basedate); 
$date2 = strtotime('+1 month', $basedate); 
$date3 = strtotime('first month', $basedate); 
$date4 = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));

$format1 = date('F', $date1);
$format2 = date('F', $date2);
$format3 = date('F', $date3);
$format4 = date('F', $date4);

echo $format1;
echo $format2;
echo $format3;
echo $format4;


Expected result:
----------------
February
February
February
February

Actual result:
--------------
March
March
March
March

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-31 16:37 UTC] scott at slerman dot net
Today is January 31st.
Adding 1 to the month would produce February 31st.
February 31st does not exist, so it is converted to March 2nd (or March 3rd during non-leap years).

The GNU date format documentation (http://www.gnu.org/software/tar/manual/html_node/tar_113.html) gives you the solution of getting the next month after the 15th of the current month. You can do this in PHP with the second parameter to strtotime:

strtotime('next month', mktime(0, 0, 0, idate('m'), 15))
 [2008-01-31 16:55 UTC] gavinp at tbs dot uk dot com
Excatly. Hence why it's a bug.

I'm asking PHP to add 1 to the month number. Not to add 31 days to the month.

strtotime('next month', $basedate); where $basedate = today

The expected result would be to produce the next month, not to add 31 days and provide the month, which may, or may not be, the next month.
 [2008-01-31 18:07 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.

just searching for "next month" in the bug system, provides the following hits in the first two pages:

http://bugs.php.net/bug.php?id=36912
http://bugs.php.net/bug.php?id=38279
http://bugs.php.net/bug.php?id=39139
http://bugs.php.net/bug.php?id=41547
http://bugs.php.net/bug.php?id=41551
http://bugs.php.net/bug.php?id=43169

granted, some of them are for "last month". It is still not a bug however, the strtotime() function adds one to the month - it does *not reset* the day of month. 2008-01-31 turns therefore into 2008-02-31, which does not exist (and therefore overflows to 2008-03-02). The same happens if we add 3 months to 2007-11-30 -> 2007-14-30 -> 2008-02-30 -> 2008-03-01 (strtotime("2007-11-30 third month") or strtotime("2007-11-30 +3 months"). What you really want to do, is:
<?php
echo date( 'm', strtotime( date( "Y-m" ) . '-01 next month' ) );
?>
 [2009-08-06 22:43 UTC] rasmus@php.net
For people who run across this one.  We went with the traditional UNIX approach for relative dates.  You can read about it here:

http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html#SEC120

It is consistent with 'date' and various other command line UNIX tools you likely use quite often.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 03:01:35 2025 UTC