seby
seby

Reputation: 15

Expired date calculation

I have found the code below on the site to calculate due date. This function seems to work on the actual day and gives me the due date based on $months. How could I use that function but base on my defined date instead. So meaning, If I enter $myday = 2013-01-01, I want the function to tell me that it is due every month (if I select Monthly of course) and in how many days it is due compared to the TODAY date. So for example, if we are January 31 2015 and I launch the function, it should tell me that the due date is in 1 day because it's every month the due date.

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

Upvotes: 0

Views: 1471

Answers (2)

Bora
Bora

Reputation: 10717

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

Upvotes: 1

nickle
nickle

Reputation: 5196

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

Upvotes: 0

Related Questions