Check if time interval is within another range

Asked

Viewed 97 times

0

I need to know if it was worked within a fixed interval

$entrada_padrao = strtotime('08:00');//fixo como parametro
$saida_padrao = strtotime('18:00');//fixo como parametro

$entrada_trabalho = strtotime('09:00');//apontamento no relogio de ponto
$saida_trabalho = strtotime('20:50');//apontamento no relogio de ponto

In this example you have to return me TRUE

I’ve tried this logic below, but it doesn’t work because so much it can enter before $entrada_padrao how much can come out after $standard date or both

if(($entrada_trabalho <= $entrada_padrao) && ($saida_trabalho >= $entrada_padrao)){
    echo "True";
}else{
    echo "False";
}
  • which is the "range fixed"? $entrada_padrao or $entrada_trabalho? I was in doubt about this, however, I suggest you edit your question and specify this. First of all, see how to create a [Mre].

  • edited, the standard input and standard output is fixed as standard. The user will point out the time worked and I will compare if he worked within the standard time, regardless of the time he entered or left

2 answers

1

In this case, you must return true only if it arrives before or at the start time and leaves at the end time or after. In this case, the work input must be less than or equal to the standard input. While the work output must be greater than or equal to the standard output. This correction should be made.

$entrada_trabalho <= $entrada_padrao && $saida_trabalho >= $saida_padrao

0

I temporarily assembled this solution

$valor = 100;//valor a pagar de bônus
$entrada_padrao = strtotime('08:00');//fixo como parâmetro
$saida_padrao = strtotime('18:00');//fixo como parâmetro

$entrada_trabalho = strtotime('09:00');//horário de entrada no trabalho
$saida_trabalho = strtotime('19:50');//horário de saída no trabalho


function calcBonus($ep, $sp, $et, $st, $v){
    if ($et >= $ep && $et <= $sp) {//se entrou dentro do horário padrão
        return $v;
    }else
    if ($et < $ep && ($st <= $sp && $st >= $ep)) {//se entrou antes do horário padrão, saiu antes da saída padrão mas não antes da entrada padrão
        return $v;
    }else
    if ($et < $ep && $st > $sp) {//entrou antes da entrada padrão e sai após o horario padrão
        return $v;
    }else {
        return 0;
    }
}

var_dump(calcBonus($entrada_padrao,$saida_padrao,$entrada_trabalho,$saida_trabalho,$valor));

If the period worked is past within the default period it returns the amount to pay. Suggestions for improvements are also welcome.

Browser other questions tagged

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