用PHP计算截止日期

时间:2012-12-12 06:49:16

标签: php datetime whmcs

我的一位同事最近在我们的结算系统中安装了一个开源脚本。目的是在客户的服务延迟付款时重新计算服务的下一个截止日期。服务示例将于2012年11月20日到期,第二天暂停,然后他们支付11/25/2012。此脚本应该将下一个截止日期更新为12/25/2012,但由于某种原因出现故障并且更改(每月)日期应该是12/25/2012至2013年1月25日。

我认为问题在于处理$ month变量的方式,但无法确定问题的位置。

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 = 32; break;
        default:                $months = 0; break;
    }

    //we return FALSE for any other billing cycles: "One Time", "Free Account" etc, they are not recurring
    if ($months == 0)
    return FALSE;

    //a bit complex calculation based on day of the month
    //exactly like native whmcs logic do
    $year = date("Y");
    $month = date("m");
    $day = date("d");
    for ($i=1; $i<=$months; $i++)
    {
        $month++;
        if ($month == 13)
        {
            $month = 1;
            $year++;
        }
    }

    if (checkdate("$month", $day, $year))
    {
        return "$year-$month-$day";
    }
    else
    {
        //getting last day of the month
        $last_day = date("t", mktime(0, 0, 0, $month, 1, $year));
        return "$year-$month-$last_day";
    }
}

3 个答案:

答案 0 :(得分:2)

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

}

答案 1 :(得分: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 = 32; break;
        default:                return FALSE;
    }

    $expires = new DateTime('plus '.$months.' months');
    $expires->modify('last day of this month');
    return $expires->format('Y-m-d');
}

答案 2 :(得分:0)

function countDueDate($policy_start_date,$months){
        $next_due_date = strtotime($policy_start_date.' + '.$months.' Months');
        if($next_due_date<time()){
            return countDueDate(date('Y-m-d', $next_due_date), $months);
        }else{
           return  date('Y-m-d',$next_due_date);
        }
    }
function getModeMonth($premium_mode){
        switch ($premium_mode){
            case 'yearly':      $q=12;break;
            case 'monthly':     $q=1;break;
            case 'quarterly':   $q=3;break;
            case 'half year':   $q=6;break;
            default :           $q=12;break;
        }
        return $q;
    }
$date=countDueDate(date('Y').'-'.date('m-d',strtotime($policy_start_date)), getModeMonth($premium_mode));