|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2003-02-28 23:34 UTC] drwav at hotpop dot com
 when strtotime is asked to create a timestaml +1 month in the future, and is given a timestamp that happens on a day that does not exist in the next month. A timestamp a few days beyond one month in the future is created.
Example:
<?php
$now = strtotime("2003-01-30");
$nextmonth = strtotime("+1 month", $now);
print date("Y-m-d", $nextmonth);
?>
the output from this script is
2003-03-02
since February has no 30th day, apparently strtotime just adds 30 days to whatever timestamp it is given to go one month in the future, hence March 2nd.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 00:00:01 2025 UTC | 
Well, we'll have to agree to disagree. A month is a period of time just like a second, a minute and an hour. It is not as well-defined since it varies from one month to the next, but on the average it is 30.5 days. Without further context, if you simply say that something will take a month or you use "a month" without specifying which month, the only correct assumption to make is that it is a period of time approximately 30.5 days. This is exactly the same problem you have if you simply add a month to the mktime() month argument without changing the day. If you tell mktime to give you the unix timestamp for Feb. 30, it is going to give you the timestamp for March 2. If what you really want is the timestamp for the 1st day of the next month, simply get the current month and add 1 to it and feed it the day in the next month you want in your mktime call. eg. $t = mktime(12,0,0,date('n')+1,10,date('Y')); That will give you the timestame for noon on the 10th day of the next month in the current year. Any change to what a month is in the current strtotime() code is going to break a lot of applications as most people want and expect the current behaviour. And relax, I was just kidding with the example. I won't actually show up and beat the crap out of you in a month.To everyone coming here trying to solve this error: A possible fix, in some cases, is "force" a date: <?php /* Scenario: I want year and month, today is 29/01. If I add one month, it will display as March not February. So I force to add by the first day of the month. date('Y-m', strtotime('+' . $i . ' months', strtotime(date('Y-m-01')))); ?>