Difference of time elapsed between two hours

0

I am trying to calculate the total hours worked of the day per employee, taking as data, time of entry, meal time, meal time and departure time. Keeping this data like this:

Entry: 08:00:00 AM

Food: 01:00:00 PM

End of meal: 01:45:00 PM

Work output: 05:00:00 PM

To calculate the hours worked in the day, I want to get the difference between the time of entry and the meal time, then the difference between the meal time when it goes back to work and the time of the end of the day of work, this I have done it in the following way:

function HorasTrabajadas($h1,$h2,$h3,$h4){

 $horalogin = new DateTime($h1);
 $horalunchstart = new DateTime($h2);
 $horalunchend = new DateTime($h3);
 $horalogout = new DateTime($h4);

 $diferencia1 = $horalogin->diff($horalunchstart);
 $diferencia2 = $horalunchend->diff($horalogout);

 $hora1 = $diferencia1->format('%H');
 $hora2 = $diferencia2->format('%H');

 $horas = $hora1+$hora2;
 return $horas . ' hours';
}

The problem is that he is giving me back the wrong differences, for example from 8 a.m. to 1 p.m. he returns me 7. And I would have to return 5, because they are 5 hours apart. I do not know why this is happening to me, I have searched and tried in other ways but without getting results. By the way, the fields of the bd are of type time.

The data was passed to the function in this way

if ($key['login'] != '00:00:00' and $key['lunch_start'] != '00:00:00' and $key['lunch_end'] != '00:00:00' and $key['logout']!='00:00:00') {
  $horas =  HorasTrabajadas($key['login'],$key['lunch_start'],$key['lunch_end'],$key['logout']);
  $form = true;
} else {
  $horas = 'The day is not complete';
  $form = false;
}

Each data is brought from the database, and each field is of type time, and it is stored in this format hh: ii: ss without more. When I bring them through the query I print them after going through the array that returns the query, the way I print them is like this:

<?php echo $key['login'] . ' AM'; ?>

And so on with the rest. And I've done echo of each field and shows them like this:

    
asked by Alejo Mendoza 15.05.2018 в 03:21
source

1 answer

2

Alejo, according to the data you give:

Entrada: 08:00:00 AM
Comida: 01:00:00 PM
Termino de comida: 01:45:00 PM
Salida de trabajo: 05:00:00 PM

The worker has worked a total time of 8 hours and 15 minutes .

One way to do the calculations is by differences in seconds, taking a timestamp of the objects.

See this code ... in the comments I show what I said above.

I have also based on DateTime::createFromFormat('H:i A', $h1) to create the date objects as you presented them in the question. That may vary, depending on how the data is stored in the database.

$h1="08:00:00 AM";
/*Si entras a las 8 tenemos horas trabajadas:
 *  1  de 08 a 09
 *  2  de 09 a 10
 *  3  de 10 a 11
 *  4  de 11 a 12
 *  5  de 12 a 13 y a las 13 paras a comer...
*/

$h2="01:00:00 PM";
$h3="01:45 PM";

/*Retomas el trabajo a las 13:45
 *  - recuerda que llevas 5 horas de trabajo
 *  más los 15 minutos hasta las 14
 * ahora seguimos contando
 * 6  de 14 a 15
 * 7  de 15 a 16
 * 8  de 16 a 17
*/
$h4="05:00:00 PM";
/*¡Al fin las 5...!
 * Trabajaste 8 horas y 15 minutos
*/
echo HorasTrabajadas($h1,$h2,$h3,$h4);


function HorasTrabajadas($h1,$h2,$h3,$h4){

    $horalogin = DateTime::createFromFormat('H:i A', $h1);
    $horalunchstart = DateTime::createFromFormat('H:i A', $h2);
    $horalunchend = DateTime::createFromFormat('H:i A', $h3);
    $horalogout = DateTime::createFromFormat('H:i A', $h4);

    $totalLunch = $horalunchstart->getTimestamp() - $horalunchend->getTimestamp();
    $totalAtWork = $horalogin->getTimestamp() - $horalogout->getTimestamp();
    $totalWorking = $totalAtWork-$totalLunch; 

    $timeWorking    = gmdate("H:i", abs($totalWorking));
    return "Trabajaste: $timeWorking";
}

Result:

Trabajaste: 08:15
    
answered by 15.05.2018 / 05:07
source