如果有一种账单支付方式,其账单设置为直接借记,其分期付款期限可能会有所不同:30/60/90天、30/45/60、15/30/45等等,我如何准确地获得分期付款日期?
到目前为止,这就是我所拥有的,但它不适用于例如30/45/60或15/45/75:
$dates = [];
$invoiceDate = explode('-', $invoiceDate); // yyyy-mm-dd Generation date
foreach ($installments as $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);
}
有了这个代码,如果我必须在2016-04-15开账单,像30/60/90这样的付款有效,返回这些付款日期:
2016年5月15日2016年6月15日2016年7月15日
然而,不是像15/45/75这样的付款:
2016年4月30日312016年5月(应为30)2016年6月30日
也没有30/45/60:
2016年5月15日312016年5月(应为30)2016年6月15日
如果账单是在2016-04-30完成的,则此付款有效:30/45/60
2016年5月30日2016年6月15日2016年6月30日
75年5月15日:也是如此
2016年5月15日2016年6月15日2016年7月15日
和30/60/90:
2016年5月30日2016年6月30日2016年7月30日
你知道如何实现一个考虑到所有可能性的算法吗?
使用DAteTime对象以日期进行操作
$periods = [ [30,60,90],
[30,45,60],
[15,30,45]];
$date = '2016-04-15';
$d = new DateTime($date);
foreach ($periods as $ps)
foreach($ps as $p) {
$t = new DateTime($d->format('Y-m-d'));
echo $t->modify('+'.$p.' days')->format('Y-m-d')."'n";
unset($t);
}
结果
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