Calculation of duration with interval

1

Good morning, I'd like to know that someone can help me create an interface in conjunction with the calculation in PHP between working hours and minutes of pause. That is, we would have an input time entered by the user and an exit time. In a separate input an hour / minute pause input.

I have tried seriously to make the necessary calculations to give correctly, but the hours and minutes minus the range, have given wrong.

I leave here some of the code I have done.

<input type="text" name="horai" id="horai" style="border: 1px solid #ccc; border-radius: 4px; padding: 6px;" required/> 
<input type="text" name="horaf" id="horaf" style="border: 1px solid #ccc; border-radius: 4px; padding: 6px;" required/>

<?php

if($_POST['horai'] > $_POST['horaf']){
    $total = (24 - $_POST['horai'] + $_POST['horaf']);
}else{
    $total = $_POST['horaf'] - $_POST['horai'];
}

$iTime= strtotime($_POST['horai']);
$fTime= strtotime($_POST['horaf']);

if($fTime> $iTime){
    $diferenca = $fTime- $iTime; 
    $semintervalo = date('H:i', $diferenca); 
} else {
    $diferenca = (24 - $iTime) + $fTime;
    $semintervalo = date('H:i', $diferenca);
}

$tempodepausa= strtotime($_POST['pausa']);
$tempodepausaaux= $diferenca- $semintervalo;
$comintervalo = date('H:i', $tempodepausaaux);

echo 'Diferença entre Horas (s): '.$diferenca.'<br/>';
echo 'Diferença entre Horas (H:m): '.$semintervalo.'<br/>';
echo 'Tempo útil (H:m): '.$comintervalo.'<br/>';
echo 'Tempo útil (s): '.$tempodepausaaux;

?>
    
asked by anonymous 16.04.2018 / 13:18

1 answer

2

I did this:

// cada servidor tem timezone diferente
// para corrigir isso, basta adicionar a linha abaixo que ficará padrão
// explicarei melhor no edit 

date_default_timezone_set('UTC'); 

$iTime= strtotime($_POST['horai']);
$fTime= strtotime($_POST['horaf']);

$tempopausaINI = strtotime("00:00:00");
$tempodepausa = strtotime($_POST['pausa']);
$tempoPausaTotal = abs($tempopausaINI - $tempodepausa);

// esse if apenas assegura que os valores formam convertidos
if ($tempodepausa !== false && $iTime !== false && $fTime !== false) {
    if($fTime > $iTime){
        $diferenca = ($fTime - $iTime); 
    } else {
        $diferenca = abs($iTime - $fTime); // o abs coloca o valor para positivo
    }
    $semintervalo = date('H:i:s', $diferenca);
    $tempodepausaaux = $diferenca - $tempoPausaTotal;
    $comintervalo = date('H:i:s', $tempodepausaaux);

    echo 'Diferença entre Horas (s): '.strtotime('1970-01-01 '.$semintervalo .'UTC').'<br/>';
    echo 'Diferença entre Horas (H:m): '.$semintervalo.'<br/>';
    echo 'Tempo útil (H:m): '.$comintervalo.'<br/>';
    echo 'Tempo útil (s): '.strtotime('1970-01-01 '.$comintervalo .'UTC');
}

The problems I identified:

1) In this line below, you try to subtract a timestamp from a date. It will not work:

$tempodepausaaux= $diferenca- $semintervalo;

2) Each hour in the timestamp has a value of 3600 integer. In the line below you subtract the number 24 from the initial time, which will only make it negative with a wrong value difference.

$diferenca = (24 - $iTime) + $fTime;

3) This condition is useless, maybe you have used it for testing:

if($_POST['horai'] > $_POST['horaf']){
    $total = (24 - $_POST['horai'] + $_POST['horaf']);
}else{
    $total = $_POST['horaf'] - $_POST['horai'];
}

4) Another problem is the pause time. When you transform the hour / minute time into timestamp without putting an initial value to make comparison. This comparison is made on the initial date of "1970-01-01". So I changed the value of:

$tempodepausa= strtotime($_POST['pausa']);

To:

$tempopausaINI = strtotime("00:00:00");
$tempodepausa = strtotime($_POST['pausa']);
$tempoPausaTotal = abs($tempopausaINI - $tempodepausa);

This will give you the correct timestamp value of the pause.

Anyway, your logic was correct, only a few details were wrong.

EDIT

strtotime by default it returns in seconds the start date January 1 1970 00:00:00 GMT by the stipulated date. However, each server can be configured with a different time zone. If you convert a this value to timestamp to date () without changing the time zone, the date / time can be changed. For if in the timestamp the initial date is on January 1 00% hours GMT , for other server this start can start later changing the expected value.

You have a change in this if too:

if($fTime > $iTime){
    $diferenca = ($fTime - $iTime); 
} else {
    $diferenca = abs($iTime - $fTime); // o abs coloca o valor para positivo
}
    
16.04.2018 / 14:30