Calculate deadlines other than on weekends

0

I will have a system that will have a days term Ex: 30 days.

I need to calculate a date given a certain deadline, which if it is weekend it is interpreted as the last business day on time.

Example:

Today: 09-12-2014, Deadline: 11 days

It should fall on 20-12-2014 (Saturday), but the system must identify and reduce the deadline to 10 days and inform the end date for the 19- 12-2014 .

    
asked by anonymous 09.12.2014 / 18:34

1 answer

1

This method calculates a deadline so that a prior deadline is not a Saturday or Sunday.

function calcularPrazo($prazoPrevio){
    $dataSTR = date('d-m-Y'); //hoje

    $data = explode('-',$dataSTR);

    //time do dia que sera o dia final do prazo previo
    $time = mktime(0, 0, 0, $data[1], intval($data[0]) + $prazoPrevio, $data[2]);

    $diaSemana = date("w", $time);

    switch($diaSemana){
        case 0: //domingo
            //subtrair mais dois dias
            $prazoPrevio -= 2;
            break;
        case 6: //sabado
            //subtrair mais um dias
            $prazoPrevio -= 1;
            break;
    }

    $time = mktime(0, 0, 0, $data[1], intval($data[0]) + $prazoPrevio, $data[2]);
    return date('d-m-Y',$time);
}
echo calcularPrazo(11);

Example working on Ideone

    
09.12.2014 / 20:15