Skip to content

Instantly share code, notes, and snippets.

@facine
Created June 19, 2019 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save facine/09e16798c035545649ac95a52b48ad5a to your computer and use it in GitHub Desktop.
Save facine/09e16798c035545649ac95a52b48ad5a to your computer and use it in GitHub Desktop.
Calculate recurring billing dates
<?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