0

我在网站上找到了以下代码来计算到期日。此功能似乎在实际日期工作,并根据 $months 为我提供截止日期。我如何使用该功能,但要基于我定义的日期。所以意思是,如果我输入 $myday = 2013-01-01,我希望函数告诉我它每个月到期(如果我当然选择每月)以及与今天日期相比它的到期天数。例如,如果我们是 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);
}
4

2 回答 2

1

Added new param $date. If you pass $date param, it'll get today.

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");

Output for 2013-01-01

2013-04-01
于 2013-09-04T10:24:20.127 回答
0

Try to send date in function then it will work take look at below code hope useful

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')
于 2013-09-04T10:24:09.863 回答