I calculate hours discounted weekends

Asked

Viewed 41 times

0

I am implementing in an application the time when a data was created in the database. This date information is created by the native function of the php time() that generates a value that is recorded in the bank as a string. In this check has an alert when pass 72 hours without handling this information, where I take the value of string of creation and we add the warning hours and check whether the value is greater than the time() current. If larger, has passed the time of treatment. So far everything works normally. What happens and what I need to deduct from the check the time of the weekends, exactly at 00:00 on Saturday until 11:59:59.

Would anyone have any idea how I can do this directly through the code? At the moment the only solution I imagined in creating a pause time in the database with the initial and final values.

1 answer

0

You can use the strtotime to subtract hours from a pre-established date:

$data_final = strtotime('-1 day', strtotime($data_sem_descanso));

Edit, the code below returns the number of days of a month subtracting the rest days:

function numDiasDescansoMes($mes, $ano, $diaDescanso1, $diaDescanso2=-1){

    $num_dias_mes = date("t", strtotime("01-$mes-$ano"));

    $n_dias_descanso = 0;

    for ($i = 1; $i <= $num_dias_mes; $i++){

        $data_loop = $i."-$mes-$ano";
        $dia_semana = date("w", strtotime($data_loop));
        if( ($dia_semana == $diaDescanso1) || ($dia_semana == $diaDescanso2) )
            $n_dias_descanso++;
    }

    return $num_dias_mes-$n_dias_descanso;

}

It’s not quite what you want, but right now I’m going away. I think that by the above code you can easily develop a function to do what you want.

  • More accurate it is automatic. Always remove Saturdays and Sundays from a timeline where you can have several weeks

  • I made something similar for a project, I’ll see if I can find.

  • See if the new code I put in helps.

  • So, everything comes on Unix timestamp. Sometimes I need to take n weeks. Not within just a month. I need to take this time line and go withdrawing Saturdays and Sundays.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.