I could not remember a PHP function that would do what you want. I had written some date stuff for a calendar, and just turned it into a function. Here is the code:
$month = date(m);
$day = date(d);
$year = date(Y);
function addMonth($year,$month,$day) {
$nextMonth = $month + 1;
//Total number of days in each month
$totalDays = array(0, "01" => 31, "02" => 28, "03" => 31, "04" => 30, "05" => 31, "06" => 30, "07" => 31, "08" => 31, "09" => 30, "10" => 31, "11" => 30, "12" => 31);
// Check for a leap year, and modify $totaldays array when needed
if (date("L", mktime(0,0,0,$month,1,$year))) {
$totalDays["02"] = 29;
}//end if
//see how many days cary over past this month
$diff = (($totalDays[$month] - $day) - 30);
//if it is the first day of a 31 day month then the month does not change
//so if $diff is zero then this is the case
if ($diff == 0) {
$newDay = $day + 30;
$newMonth = $month;
//if more days carry over then are in the next month we add two months
// and adjust the day accordingly
}elseif($diff > $totalDays[$nextMonth]){
$newDay = ($diff - $totalDays[$nextMonth]);
$newMonth += 2;
//otherwise the $diff is less than the total days in the next month
//we update the day and month accordingly
}else{
$newDay = $diff;
$newMonth = ($month +1);
}
//if we are moving forward a year then we must update that value
if ($month == 12 && $newMonth < 12) {
$newYear = $year + 1;
//otherwise we keep the year the same
}else{
$newYear = $year;
}
$date = $newYear . "-" . $newMonth . "-" . $newDay;
return $date;
}
$arg = addMonth($year,$month,$day);
print $year . "-" . $month . "-" . $day . "<br>";
print $arg;
DATE_ADD() is a mysql function, not a PHP function, if your date is comming from a mysql database you could use DATE_ADD in the select statment. Mysql has some pretty cool date funcitons that do not generaly have a PHP equivalent
Hope this helps.