0

I'm trying to make a script that given a date adds a number of dates based on $pedidos/$pedidos_capacidad and if it's sunday or saturday, skip that day and still counting but it's not working for me.. This is what I got so far:

<?php

/*Check if it's not sunday or saturday*/
function es_laborable($date) {
    $dia_actual = date('w', strtotime($date));
    return ($dia_actual != 0 || $dia_actual != 6);
}

/*Generates the corresponding date*/
function dias_envio($pedidos, $pedidos_capacidad, $fecha){

    $dias = $pedidos / $pedidos_capacidad;

$dias = round($dias);

    $fecha_envio = $fecha;

    while($dias != 0){
        if(!es_laborable($fecha_envio)){
            $fecha_envio = date('Y/m/d', strtotime($fecha. ' + 1  days'));
            $dias--;
        }
        else{
            $fecha_envio = date('Y/m/d', strtotime($fecha. ' + 1  days'));
        }
    }

//Adds one more working day to resulting date (checking if result is not sunday or saturday).
$fecha_envio = date('Y/m/d', strtotime($fecha. ' + 1 days'));
$es_laborable = es_laborable($fecha_envio);
while(!$es_laborable){
    $fecha_envio = date('Y/m/d', strtotime($fecha. ' + 1 days'));
    $es_laborable = es_laborable($fecha_envio);
}        

    return $fecha_envio;
}

/*Calling function*/
$pedidos = 19;
$pedidos_capacidad = 4;
$fecha = '2018/02/07';

$fecha_envio = dias_envio($pedidos, $pedidos_capacidad, $fecha);

echo $fecha_envio;
?>

Does anyone know how to solve this?

UPDATE:

errorlog shows 'Maximum execution time reached'.

UPDATE2:

I found that the only issue is that es_laborable function is not checking if they are working days or not..

$es_laborable = es_laborable($fecha_envio);
while(!$es_laborable){
    $fecha_envio = date('Y/m/d', strtotime($fecha. ' + 1 days'));
    $es_laborable = es_laborable($fecha_envio);
}  

it doesn't add a day if result is saturday or sunday. What's happening?

molinet
  • 266
  • 3
  • 14

0 Answers0