phpdatedatetimestrtotimeauto-renewing

PHP get the Date of the Termination of a contract that is renewed automatically based on the Resignation date


I want to program a function that based on the given Date, get the date ($end_date) of the contract termination.

And the conditions are:

On the first 3 Months after contract creation we have a grace periode. So if we receive the contract resignation until 14 days before the end of the first 3 Months, the contract duration will not apply and the termination is valid to the last days of the first 3 months after contract creation.

If the grace periode is gone the contract duration will apply, so the resignation is only valid if we receive it 6 months before the contract duration ends. If the we receive the resignation after 6 months to the end of the contract duration, the contract will be renewed for the same contract duration.

I have this information:

$resignation_date =  date('Y-m-d', strtotime($letter_of_resignation_received_date));

$contract_begin_date = date('Y-m-d', strtotime($contract_begin_date));

$contract_duration= 48; //This means 48 Months

1st. condition:

$grace_periode_date= date('Y-m-d', strtotime($contract_begin_date . ' + 3 months - 14 days'));

if ($resignation_date \< $grace_periode_date ) {
$end_date = date('Y-m-d', strtotime($contract_begin_date . ' + 3 months'));
}
  1. condition:

    if ($resignation_date > $grace_periode_date ) {
      $end_date = date('Y-m-d', strtotime($contract_begin_date . ' + '.$contract_duration.' months'));
    }

  1. condition:

if ($resignation_date \> $grace_periode_date && $resignation_date \> date('Y-m-d', strtotime($contract_begin_date . ' + '.$contract_duration.' months - 6 months'))) {
$end_date = date('Y-m-d', strtotime($contract_begin_date . ' + '.$contract_duration.' months + '.$contract_duration.' months '));
}

All this is working good. The problem is if the contract is for exemple alerady automatically renewed for 2 times, then the way how im doing this is not the correct way. Im all the time based on the $contract_begin_date + the new duration.

So if the contract is already renews for 2 times the 2nd. an 3th. condition will deliver a wrong date. Because I have not the date of the last renew, i have only the contract begin date and the duration of the contract.

How can I get the correct end_date only using the information that I have?


Solution

  • You need to calculate the start of the contract period in which you receive contract resignation.

    function getContractPeriodStart($constractStartDate, $contractDurationInMonths, $targetDate)
    {
        $periodEnd = strtotime($constractStartDate);
        do {
            $periodStart = $periodEnd;
            $periodEnd   = strtotime(date('Y-m-d', $periodStart) .   
                                     ' +' . $contractDurationInMonths . ' months');
        } while ($periodEnd < strtotime($targetDate));
        $return date('Y-m-d', $periodStart);
    }
    

    Here the $targetDate is the date you received the resignation.

    The idea is simple: Just walk forwards through the contract periods until the end of a period is beyond the date you're interested in.

    After this you can use the start of the period, instead of the start of the contract for your third condition.

    Note: Code is untested and may contain errors.