Skip to content

Instantly share code, notes, and snippets.

@Tobur
Created March 31, 2014 20:33
Show Gist options
  • Save Tobur/9901594 to your computer and use it in GitHub Desktop.
Save Tobur/9901594 to your computer and use it in GitHub Desktop.
<?php
/**
* @return DateTime
*/
function getNextPaymentDate(DateTime $lastPaymentDate = null)
{
// 1. Get start date
$day = 1;
// 2. Get now
$now = new DateTime();
// 3. Get current day, month, year
$currentDay = $now->format('d');
$currentMonth = $now->format('m');
$currentYear = $now->format('Y');
var_dump($now);
if ($currentDay > 1) { // 4. If payment day has already gone, we should take next month
$now->modify('+1 month');
$month = $now->format('m');
$year = $now->format('Y');
var_dump($now);
// 5. If due date is today and payment has been made today, we should move to next month
} elseif (($currentDay == 1)
&& $lastPaymentDate
&& $lastPaymentDate->format('Ymd') == $now->format('Ymd')
) {
$now->modify('+1 month');
$month = $now->format('m');
$year = $now->format('Y');
} else { // 5. If the day is Ok, month and year should be the current ones
$month = $currentMonth;
$year = $currentYear;
}
$daysInMont = cal_days_in_month(CAL_GREGORIAN, $month, $year);
if ($day > $daysInMont) {
$day = $daysInMont;
}
var_dump($year);
var_dump($month);
var_dump($day);
exit;
return new DateTime(implode('-', array($year, $month, $day)));
}
$date = DateTime::createFromFormat('Y-m-d', '2014-02-28');
$date2 = getNextPaymentDate($date);
var_dump($date2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment