0

I need to calculate the difference of hours worked between two dates

ex:

$dateStart = '201705151330';
$dataEnd = '201705161230';  
$lunchStart = '12:00';  
$lunchEnd = '13:30'; 

Until then I can solve with the following code:

$dateStart = '201705151330';
$dataEnd = '201705161230';  
$lunchStart = '12:00';  
$lunchEnd = '13:30'; 

$datatime1 = new DateTime($dateStart);
$datatime2 = new DateTime($dataEnd);


$data1  = $datatime1->format('Y-m-d H:i:s');
$data2  = $datatime2->format('Y-m-d H:i:s');  

$data1 = strtotime($data1);
$data2 = strtotime($data2);

$nHours   = ($data2 - $data1) / 3600;
$nMinutes = (($data2 - $data1) % 3600) / 60;
$total = sprintf('%02d:%02d', $nHours , $nMinutes);

echo $total;

But I need to take into account that the shift is from 07:30 to 12:00 and from 13:30 to 17:48, so I need to deduct lunchtime and hours not worked.

M Santos
  • 1
  • 1
  • Although it doesn't answer the question, this library will change your life. It's the easiest way I've found to manage dates with PHP: http://carbon.nesbot.com/docs/ – Jeremy Harris May 16 '17 at 14:10
  • The questions mentioned do not take into account the hours worked, they only calculate the difference of hours between two dates. I'm looking for something like days and still have not found. – M Santos May 16 '17 at 14:15

0 Answers0