I have two columns in my db: start_date
and end_date
, which are both DATE
types. My code is updating the dates as follows:
This function returns any correct number of months positively or negatively. Found in the comment section here:
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;
}