How to avoid having holidays

3

Hello, I have an application so that a user can take vacation days. I have a function that does not tell me neither weekend nor holidays. For example in Andalusia on March 29 is a holiday, if a user takes a vacation from March 28 to April 2, it will come out that he has taken 3 days of vacation because he has not counted either the holiday or the end of week. So far so good. My problem comes when I select nothing more than March 29. If I put that day's holidays only and choose March 29 at 08:00 until March 29 at 5:00 p.m., tell me that day as 1 instead of telling it as 0. Any help? Here I leave my function:

function diashabiles($fechainicio, $fechafin){
    $inicio = strtotime($fechainicio);
    $final = strtotime($fechafin);

    $dias = 0;

    $sql = "SELECT COUNT(*) FROM fechas as f join calendarios as c on c.PK_CALENDARIOS = f.FK_CALENDARIO join sec_users as u on u.fk_zona = c.PK_CALENDARIOS WHERE u.login = 'user1' AND f.FECHA_ZONA between '".$fechainicio."' AND '".$fechafin."'";
    sc_lookup(rr, $sql);

    while(date('Y-m-d', $inicio) <= date('Y-m-d', $final)){
        $dias += date('N', $inicio) < 6 ? 1 : 0;
        $inicio = strtotime("+1 day", $inicio);
    }

    $dias -= {rr[0][0]};

    return $dias;
}
    
asked by Xerox 23.03.2018 в 08:16
source

1 answer

2

You can give a fixed format to the dates (start date, end date). After all you are not interested in hours since the result is days ( diashabiles ). The error can give you when the starting date / date does not cover the day you have set as a day in "fecha.FECHA_ZONA".

Your code would be as follows:

function diashabiles($fechainicio, $fechafin){
    $fechainicio= date('Y-m-d 00:00:00', strtotime($fechainicio));
    $fechafin= date('Y-m-d 23:59:59', strtotime($fechafin));
    //-- Acá continúa tu código.
    $inicio = strtotime($fechainicio);
    $final = strtotime($fechafin);

    $dias = 0;

    $sql = "SELECT COUNT(*) FROM fechas as f join calendarios as c on c.PK_CALENDARIOS = f.FK_CALENDARIO join sec_users as u on u.fk_zona = c.PK_CALENDARIOS WHERE u.login = 'user1' AND f.FECHA_ZONA between '".$fechainicio."' AND '".$fechafin."'";
    sc_lookup(rr, $sql);

    while(date('Y-m-d', $inicio) <= date('Y-m-d', $final)){
        $dias += date('N', $inicio) < 6 ? 1 : 0;
        $inicio = strtotime("+1 day", $inicio);
    }

    $dias -= {rr[0][0]};

    return $dias;
}
    
answered by 23.03.2018 / 08:44
source