Created
October 23, 2016 11:00
-
-
Save cougar999/982b0ed5232f1e8c50235859502ab46c to your computer and use it in GitHub Desktop.
calculate Month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_months($months, DateTime $dateObject) | |
{ | |
$next = new DateTime($dateObject->format('Y-m-d')); | |
$next->modify('last day of +'.$months.' month'); | |
if($dateObject->format('d') > $next->format('d')) { | |
return $dateObject->diff($next); | |
} else { | |
return new DateInterval('P'.$months.'M'); | |
} | |
} | |
function endCycle($d1, $months) | |
{ | |
$date = new DateTime($d1); | |
// call second function to add the months | |
$newDate = $date->add(add_months($months, $date)); | |
// goes back 1 day from date, remove if you want same day of month | |
$newDate->sub(new DateInterval('P1D')); | |
//formats final date to Y-m-d form | |
$dateReturned = $newDate->format('Y-m-d'); | |
return $dateReturned; | |
} | |
example: | |
$startDate = '2014-06-03'; // select date in Y-m-d format | |
$nMonths = 1; // choose how many months you want to move ahead | |
$final = endCycle($startDate, $nMonths) // output: 2014-07-02 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment