Created
June 19, 2019 19:08
-
-
Save facine/09e16798c035545649ac95a52b48ad5a to your computer and use it in GitHub Desktop.
Calculate recurring billing dates
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
<?php | |
// Create a new DateTime object with the first billing date. | |
$lastDate = new DateTime('2019-01-31'); | |
// Clone the date object or the original date will be altered. | |
$nextDate = clone $lastDate; | |
$periodUnit = 'M'; | |
// Add the number of months. | |
$numberMonths = 1; | |
$nextDate->add(new \DateInterval('P' . $numberMonths . $periodUnit)); | |
// Check the months amount and correct if it is needed. | |
if (intval($nextDate->format('n')) > (intval($lastDate->format('n')) + $numberMonths) % 12 && | |
(intval($lastDate->format('n')) + $numberMonths) != 12) { | |
$nextDate->modify('last day of -1 month'); | |
} | |
echo $nextDate->format('Y-m-d'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment