过期日期计算

时间:2013-09-04 10:16:29

标签: php date

我在网站上找到了以下代码来计算截止日期。此功能似乎适用于实际日期,并根据$ 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);
}

2 个答案:

答案 0 :(得分:1)

添加了新的参数$date。如果你通过$date param,它今天就会到来。

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

答案 1 :(得分:0)

尝试在功能中发送日期然后它将工作看下面的代码希望有用

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')