2
I need to calculate the working time difference between two dates
ex:
$dataIni = '201705151330';
$dataFim = '201705161230';
Until then I can solve with the following code:
$dataIni = '201705151330';
$dataFim = '201705161230';
$datatime1 = new DateTime($dataIni);
$datatime2 = new DateTime($dataFim);
$data1 = $datatime1->format('Y-m-d H:i:s');
$data2 = $datatime2->format('Y-m-d H:i:s');
$data1 = strtotime($data1);
$data2 = strtotime($data2);
$nHoras = ($data2 - $data1) / 3600;
$nMinutos = (($data2 - $data1) % 3600) / 60;
$total = sprintf('%02d:%02d', $nHoras , $nMinutos);
echo $total;
But I have to take into account that the shift is 07:30 until the 12:00 and of 13:30 until the 17:48, or it is necessary to discount lunch and hours not worked. How can I resolve this in PHP?
So show me what you’ve done
– user60252
How can an event start at 13:30 and end at 12:30 on the same day? Please log in to [Edit] and add what is your code so far, as indicated in the comment above, and add a [mcve], that is, start and end values that make sense, as well as the expected result when the input is these values.
– Woss
Okay, include my code and fix the example periods.
– Marcos Santos
Before you had put the dates of the same day, which implied that you had been wrong about $dataIni and $dataFim what induced me to this script.
– user60252
Yes, before it was wrong as quoted by @Anderson-carlos-woss but I changed it right away.
– Marcos Santos
and the $dataIni and $dataFim can be any times and any days?
– user60252
Yes, they can be at different times and days, the application I am developing has the purpose of lifting the amount of working hours that a machine was stopped due to maintenance.
– Marcos Santos
This is more of a logic problem than PHP. What you need is to change the way Voce is solving the problem. There is a minimum period of 8 hours that the guy has to work. What you need to understand now are the extras, which happen when the guy enters BEFORE at 7:30 and leaves AFTER 17:48 Basically, Hras = 8 + (hraEntrada - 7:30) + (17:48 - hraSaida)
– Raphael Ramos
I’ve already solved it, thank you. I just won’t post the code because it’s too long.
– Marcos Santos
very cool this, if someone had solved your problem and not posted because it is too extensive I think you would not be curious not right, alias mine was not so extensive, but you already had your solution and that’s enough
– user60252