Created
October 27, 2014 20:39
-
-
Save caferrari/2cb06602673dd684d5fa to your computer and use it in GitHub Desktop.
Next pay date
This file contains hidden or 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 | |
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