Skip to content

Instantly share code, notes, and snippets.

@caferrari
Created October 27, 2014 20:39
Show Gist options
  • Save caferrari/2cb06602673dd684d5fa to your computer and use it in GitHub Desktop.
Save caferrari/2cb06602673dd684d5fa to your computer and use it in GitHub Desktop.
Next pay date
<?php
function getNextPayDate($day, DateTime $startDate = null)
{
$date = new DateTime;
if (null !== $startDate) {
$date = clone $startDate;
}
$nextMonth = $date->format('m') + 1;
if (13 == $nextMonth) {
$nextMonth = 1;
}
$date->modify("+1 month");
while ((int)$date->format('m') != $nextMonth) {
$date->modify('-1 day');
}
return $date;
}
$startDate = DateTime::createFromFormat('Y-m-d', '2014-01-31');
$date = getNextPayDate(31, $startDate);
echo $startDate->format('d/m/Y') . ' - ' . $date->format('d/m/Y') . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment