我在网站上找到了下面的代码来计算截止日期。这个函数似乎在实际日期工作,并根据$months为我提供截止日期。我如何使用该功能,而是基于我定义的日期。所以意思是,如果我输入 $myday = 2013-01-01,我希望函数告诉我它每个月到期(当然,如果我选择每月)以及与 TODAY 日期相比到期的天数。例如,如果我们是 2015 年 1 月 31 日并且我启动了该函数,它应该告诉我截止日期为 1 天,因为它是每个月的截止日期。
function calculate_postpone_due_date($billingcycle) {
switch ($billingcycle) {
case "Monthly":
$months = 1;
break;
case "Quarterly":
$months = 3;
break;
case "Semi-Annually":
$months = 6;
break;
case "Annually":
$months = 12;
break;
case "Biennially":
$months = 24;
break;
case "Triennially":
$months = 36;
break;
default:
$months = 0;
break;
}
if ($months == 0) {
return false;
}
$today = date('Y-m-d');
$next_due_date = strtotime($today . ' + ' . $months . ' Months');
return date('Y-m-d', $next_due_date);
}
添加了新的参数$date
。如果你传递$date
参数,它就会得到今天。
function calculate_postpone_due_date($billingcycle, $date = false)
{
switch($billingcycle)
{
case "Monthly": $months = 1; break;
case "Quarterly": $months = 3; break;
case "Semi-Annually": $months = 6; break;
case "Annually": $months = 12; break;
case "Biennially": $months = 24; break;
case "Triennially": $months = 36; break;
default: $months = 0; break;
}
if ($months == 0)
return FALSE;
$current = $date ? date('Y-m-d', strtotime($date)) : date('Y-m-d');
$next_due_date = strtotime($current.' + '.$months.' Months');
return date('Y-m-d', $next_due_date);
}
echo calculate_postpone_due_date("Quarterly", "2013-01-01");
2013-01-01的产出
2013-04-01
尝试在函数中发送日期,然后它将起作用,请查看下面的代码希望有用
function calculate_postpone_due_date($billingcycle,$date)
{
switch($billingcycle)
{
case "Monthly": $months = 1; break;
case "Quarterly": $months = 3; break;
case "Semi-Annually": $months = 6; break;
case "Annually": $months = 12; break;
case "Biennially": $months = 24; break;
case "Triennially": $months = 36; break;
default: $months = 0; break;
}
if ($months == 0)
return FALSE;
$today = $date;
$next_due_date = strtotime($today.' + '.$months.' Months');
return date('Y-m-d', $next_due_date);
}
echo calculate_postpone_due_date($billingcycle,'your date')