Since this seems to be a fairly confusing topic here is some information on it:
You are actually getting an accurate result it is literally increasing the month by 1, the day remains 31, therefore the date is 2011-06-31. If you do echo date('Y-m-d', strtotime('2011-06-31'));
you'll see it displays 2011-07-01
.
Here is one method of making this work as "expected" in PHP 5.1 (and before)
function next_month($timestamp)
{
$next_month = date('m', $timestamp);
$next_month++;
$next_year = date('Y', $timestamp);
if($next_month == 12)
{
$next_year++;
}
if(date('d', $timestamp) <= date('t', mktime(0, 0, 0, $next_month, 1, $next_year)))
{
return date('Y-m-d',strtotime( '+1 month', $timestamp));
}
else
{
return date('Y-m-d', mktime(0, 0, 0, $next_month, date('t', mktime(0, 0, 0, $next_month, 1, $next_year)), $next_year));
}
}
echo next_month(strtotime('2011-05-31'));
echo next_month(strtotime('2011-05-01'));
This is modified code from a library I wrote a while ago -- I've never found an elegant solution.
For PHP 5.3+
Refer to PHP DateTime::modify adding and subtracting months for a detailed Q/A on this topic.