Calculate the working time difference between two dates

Asked

Viewed 942 times

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

  • 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.

  • Okay, include my code and fix the example periods.

  • 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.

  • Yes, before it was wrong as quoted by @Anderson-carlos-woss but I changed it right away.

  • and the $dataIni and $dataFim can be any times and any days?

  • 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.

  • 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)

  • I’ve already solved it, thank you. I just won’t post the code because it’s too long.

  • 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

Show 5 more comments

1 answer

1


You can solve this problem by using the native classes PHP offers to manipulate Date/Time.

According to the description of your problem a possible solution would be to calculate the number of minutes between two dates where you should consider only the period that lies between the defined shifts:

<?php

$dataIni = '201705151330';
$dataFim = '201705161230';

$datatime1 = new DateTime($dataIni);
$datatime2 = new DateTime($dataFim);

$intervaloEmMinuto = new DateInterval('PT1M');
$periodo = new DatePeriod($datatime1, $intervaloEmMinuto, $datatime2);
$minutos = 0;
foreach ($periodo as $data) {
        /* @var $data \DateTime */
        $dataEmMinuto = clone $data;

        $inicioDoPrimeiroTurno = clone $dataEmMinuto->setTime(7, 30, 0);
        $fimDoPrimeiroTurno = clone $dataEmMinuto->setTime(12, 0, 0);
        $inicioDoSegundoTurno = clone $dataEmMinuto->setTime(13, 30, 0);
        $fimDoSegundoTurno = clone $dataEmMinuto->setTime(17, 48, 0);

        if (($inicioDoPrimeiroTurno < $data && $data < $fimDoPrimeiroTurno) || ($inicioDoSegundoTurno < $data && $data < $fimDoSegundoTurno)) {
                $minutos++;
        }
}

$intervalo = new DateInterval("PT{$minutos}M");
$data = new DateTime();
$dataAtual = clone $data;
$data->add($intervalo);
$horasUteisEntreDuasDatas = $dataAtual->diff($data);
echo 'Horas úteis entre duas datas: '. $horasUteisEntreDuasDatas->format('%d dias %H horas %i minutos');

Browser other questions tagged

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