php - Formula to get payment dates of direct debit bills -
having billing payment method bills set direct debit, , installment periods may vary: 30/60/90 days, 30/45/60, 15/30/45 , on, how can accurately installment dates?
this have far, doesn't work instance 30/45/60, or 15/45/75:
$dates = []; $invoicedate = explode('-', $invoicedate); // yyyy-mm-dd generation date foreach ($installments $i => $installment) { // e.g.: $installment = [30, 45, 60] if ($installment % 30 == 0) { $month = round($installment / 30); $date = mktime(0, 0, 0, $invoicedate[1] + $month, $invoicedate[2], $invoicedate[0]); } else { $month = 0; if ($installment > 30 && count($installments) > 1) { $month = floor($installment / 30); $installment = abs($installment - ($month * 30)); } $monthoffset = 30 - date('t', mktime(0, 0, 0, $invoicedate[1] + $month, 1, date('y'))); $date = mktime(0, 0, 0, $invoicedate[1] + $month, (int)$invoicedate[2] + (int)$installment - $monthoffset, $invoicedate[0]); } $dates[] = date('y-m-d', $date); }
with code, if had bill on 2016-04-15, payments 30/60/90 work, returning these payment dates:
15/05/2016 15/06/2016 15/07/2016
however, not payments 15/45/75:
30/04/2016 31/05/2016 (should 30) 30/06/2016
nor 30/45/60:
15/05/2016 31/05/2016 (should 30) 15/06/2016
if billing done in 2016-04-30, payment works: 30/45/60
30/05/2016 15/06/2016 30/06/2016
so 15/45/75:
15/05/2016 15/06/2016 15/07/2016
and 30/60/90:
30/05/2016 30/06/2016 30/07/2016
any idea how implement algorithm takes account possibilities?
use datetime object manipulate date
$periods = [ [30,60,90], [30,45,60], [15,30,45]]; $date = '2016-04-15'; $d = new datetime($date); foreach ($periods $ps) foreach($ps $p) { $t = new datetime($d->format('y-m-d')); echo $t->modify('+'.$p.' days')->format('y-m-d')."\n"; unset($t); }
result
2016-05-15 2016-06-14 2016-07-14 2016-05-15 2016-05-30 2016-06-14 2016-04-30 2016-05-15 2016-05-30
Comments
Post a Comment