Calculation of interval duration

Asked

Viewed 153 times

1

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

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

I leave here a little of the code I have made.

<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;

?>
  • And how should these inputs be entered? Can I type 08:00 for initial and 15:00 for final? or 16 April 23:00 and 17 April 2:00 that the code will work properly?

  • 10:00 - 15:00 of the same day works. For different days I have not tested why I separated the code.

  • My PC code works perfectly. I don’t understand how it gives you those values. See if type="date" or "datetime"

  • Yes, you have to type correctly in the format ano-mes-dia hh:mm:ss to work properly. Hassle when typing!!!

  • On your pc to start 2018-04-16 8:00:00 to the end 2018-04-17 8:00:00 (24 hours) and 1 hour interval when returns?

  • 1

    @Leocaracciolo the problem is not in the code itself. The problem is in the time_zone. I will edit the answer.

  • 1

    @Andreicoelho, I’m not asking about the code, I’m asking about the input of data into the inputs that are text type. This way the user will have to be soothsayer or keep trying, trying ....

  • @Leocaracciolo yes.. I understood. But take the test now. It won’t change the value. The problem was just that. Anyway, thank you. As I had not tested on another server, this would bring problems.

  • 1

    @Andreicoelho, blz, my purpose was to help AP validate the inputs with the inputs correctly so that the code works. Note that any type of entry is accepted. But anyway .... never mind .... lacked interest on his part

  • 2

    @Leocaracciolo yes! That’s right! It’s important that he validates BEFORE starting this process.

  • 1

    I use a Picker data in the middle and validates me automatically. This was all just to get to know which calculations were wrong. Andrei helped a lot.

  • 1

    @White quiet friend!

Show 7 more comments

1 answer

2


I did it this way:

// 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 row below, you try to subtract a timestamp from a date. It will not work:

$tempodepausaaux= $diferenca- $semintervalo;

2) Each time in timestamp has a value of 3600 integer. In the row 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, you may 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 turn the hour/minute time into timestamp without putting an initial value to compare. 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);

Thus, you will have the correct value in pause timestamp.

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

EDIT

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

There’s a change in this if also:

if($fTime > $iTime){
    $diferenca = ($fTime - $iTime); 
} else {
    $diferenca = abs($iTime - $fTime); // o abs coloca o valor para positivo
}
  • 1

    Thank you very much Andrei. The 3) was in case to pass for example from 16 April 23:00 to 17 April 2:00 in the morning. O 2) I don’t know how I didn’t realize, I must have been very confused. Otherwise I understood perfectly.

  • @White quiet, if the answer is correct, do not forget to put it as right. Hug!

  • 1

    It’s perfect! Is it already right? Finally, it works. Thank you Andrei, if you need to recommend even more just say.

  • @White is great friend! Thank you! Hug!

Browser other questions tagged

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