This works:
<?php
$StartDateline = new DateTime("31 January 2011");
echo date('Y-m-d', $StartDateline->getTimestamp());
echo "<br>";
$test = $StartDateline->modify('last day of next month');
echo date('Y-m-d', $test->getTimestamp());
echo "<br>";
?>
result:
2011-01-31
2011-02-28
Here's a similar answer that exaplains why your way doesn't give the wanted output.
PHP docs on DateTime::modify method
edit:
I understand now that you don't need the last day of next month, but the same day of next month.
I found this code working:
function addMonthsToTime($numMonths = 1, $timeStamp = null){
$timeStamp === null and $timeStamp = time();//Default to the present
$newMonthNumDays = date('d',strtotime('last day of '.$numMonths.' months', $timeStamp));//Number of days in the new month
$currentDayOfMonth = date('d',$timeStamp);
if($currentDayOfMonth > $newMonthNumDays){
$newTimeStamp = strtotime('-'.($currentDayOfMonth - $newMonthNumDays).' days '.$numMonths.' months', $timeStamp);
} else {
$newTimeStamp = strtotime($numMonths.' months', $timeStamp);
}
return $newTimeStamp;
}
$timestamp = strtotime('2011-01-01');
echo date('m/d/Y', addMonthsToTime(1, $timestamp));
//02/01/2011
$timestamp = strtotime('2011-01-31');
echo date('m/d/Y', addMonthsToTime(1, $timestamp));
//02/28/2011
taken from this comment section on PHP's Relative Formats doc page.