15

I'm using date()and strtotime() functions to display the next 3 months in a dropdown list.

PHP Code:

   echo date("m/Y",strtotime("+0 months")); 
   echo date("m/Y",strtotime("+1 months"));
   echo date("m/Y",strtotime("+2 months")); 

However, if the script is running when the server date is on 30th or 31st, the next month, which is Feburary, will be displayed as March instead. i.e. the script above is supposed to return

01/2012
02/2012
03/2012

But, instead of that, it actually displays

01/2012
03/2012
03/2012

that is because Feburary doesn't have 30th or 31st, so the script translates "31/02" into "01/03".

I have read the strtotime() page on php.net, this problem has been raised, but there has not been any useful solutions. So can anyone please help me to find a simple way to solve this problem? Thanks in advance!

Michael Wu
  • 293
  • 1
  • 4
  • 13
  • 1
    possible duplicate of [strtotime returns incorrect timestamp for '-1 month'](http://stackoverflow.com/questions/5481949/strtotime-returns-incorrect-timestamp-for-1-month) and [PHP Strtotime -1month -2month](http://stackoverflow.com/questions/1211824/php-strtotime-1month-2month) and [How many days does 1 month represent in PHP?](http://stackoverflow.com/questions/5464221/how-many-days-does-1-month-represent-in-php) and [strtotime 'next month' not acting as expected (today that is)](http://stackoverflow.com/questions/6182925/strtotime-next-month-not-acting-as-expected-today-that-is) – animuson Jan 30 '12 at 02:16

4 Answers4

31

As mentioned within the documentation, you should pass the date for the first day of the current month as the second parameter to the strtotime() function:

$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
echo date('m/Y',strtotime('+0 month', $base));
echo date('m/Y',strtotime('+1 month', $base));
echo date('m/Y',strtotime('+2 month', $base));

See that it works: http://ideone.com/eXis9

01/2012

02/2012

03/2012

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198
12

Try using "first day of" in your strtotime, like this:

strtotime("first day of +1 month");

This will fix dates ( in the event today was Jan 30th ) such as 02-30 (Yields march 2nd) by converting it to 02-01 (Feb 1st) which then gives you the correct month. It's a little cleaner than other methods, and easier to remember.

SeanDowney
  • 17,368
  • 20
  • 81
  • 90
5
echo date('m/Y', strtotime(date('Y-m') . '-01 +2 months'));

Just hard-code it to be the first of the month.

Brad
  • 159,648
  • 54
  • 349
  • 530
0

Don't use strtotime() for getting offset date by month(s). It works properly only in PHP 5.3+. The best way to solve such problem is using mktime(). Below is a sample code:

function getOffsetByMonths($nMonths, $nNow = 0) {
    if ($nNow)
        return mktime(0, 0, 0, date('n', $nNow)+ $nMonths, 1, date('Y', $nNow));
    else
        return mktime(0, 0, 0, date('n')+ $nMonths);
}
$nNow = mktime(0, 0, 0, 1, 31, 2013);
echo "Now: ". date("Y-m-d", $nNow).
"<br>(Now - 1 month): ". date("Y-m", getOffsetByMonths(-1, $nNow)). "-xx".
"<br>(Now - 2 month): ". date("Y-m", getOffsetByMonths(-2, $nNow)). "-xx".
"<br>(Now - 3 month): ". date("Y-m", getOffsetByMonths(-3, $nNow)). "-xx";
hindmost
  • 7,125
  • 3
  • 27
  • 39