Skip to content

Instantly share code, notes, and snippets.

@johnfuller
Created March 3, 2011 08:13
Show Gist options
  • Save johnfuller/852493 to your computer and use it in GitHub Desktop.
Save johnfuller/852493 to your computer and use it in GitHub Desktop.
Helpful functions to figure out future dates to days, months, years. Stolen from PHP.net comments for mktime.
<?php
public function addMonthToDate($timeStamp, $totalMonths=1){
// You can add as many months as you want. mktime will accumulate to the next year.
$thePHPDate = getdate($timeStamp); // Covert to Array
$thePHPDate['mon'] = $thePHPDate['mon']+$totalMonths; // Add to Month
$timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']); // Convert back to timestamp
return $timeStamp;
}
public function addDayToDate($timeStamp, $totalDays=1){
// You can add as many days as you want. mktime will accumulate to the next month / year.
$thePHPDate = getdate($timeStamp);
$thePHPDate['mday'] = $thePHPDate['mday']+$totalDays;
$timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']);
return $timeStamp;
}
public function addYearToDate($timeStamp, $totalYears=1){
$thePHPDate = getdate($timeStamp);
$thePHPDate['year'] = $thePHPDate['year']+$totalYears;
$timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']);
return $timeStamp;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment