-3
I have the following situation, a method that calculates the difference between the beats of a point and the sum of intervals that the collaborator performs during his working day:
//calcula as diferenças das batidas do ponto diário do colaborador
$d1 = gmdate('H:i', strtotime('2021-08-06 15:00') - strtotime('2021-08-06 12:00'));
$d2 = gmdate('H:i', strtotime('2021-08-06 15:30') - strtotime('2021-08-06 15:00'));
$d3 = gmdate('H:i', strtotime('2021-08-06 17:00') - strtotime('2021-08-06 15:30'));
$d4 = gmdate('H:i', strtotime('2021-08-06 17:30') - strtotime('2021-08-06 17:00'));
$d5 = gmdate('H:i', strtotime('2021-08-06 21:00') - strtotime('2021-08-06 17:30'));
//subtotal: soma dos horários trabalhados
$minutos = date("i", strtotime($d3));
$hora = date("H", strtotime($d3));
$subtotal = strtotime("+$minutos minutes", strtotime($d1));
$subtotal = strtotime("+$hora hours", $subtotal);
$minutos = date("i", strtotime($d5));
$hora = date("H", strtotime($d5));
$subtotal = strtotime("+$minutos minutes", $subtotal);
$subtotal = strtotime("+$hora hours", $subtotal);
//intervalo: soma de dos intervalos realilizados
$minutos = date("i", strtotime($d4));
$hora = date("H", strtotime($d4));
$interval = strtotime("+$minutos minutes", strtotime($d2));
$interval = strtotime("+$hora hours", $interval);
//calculo p/ horas a mais ou devendo
if (strtotime($subtotal) >= strtotime("08:00")) {
$extra = strtotime("-8 hours", $subtotal);
} else {
//N CAI AQUI!!!
}
$data = [
'd1' => $d1,
'd2' => $d2,
'd3' => $d3,
'd4' => $d4,
'd5' => $d5,
'subtotal' => "Subtotal: " . date("H:i", $subtotal),
'intervalo' => "Intervalo: " . date("H:i", $interval),
'extra' => "Extra: " . date("H:i", $extra)
];
echo '<pre>';
print_r($data);
echo '</pre>';
It happens that on my parole, if the collaborator has a subtotal of 07:50, this is not analyzed by the operator else
.
I don’t know what’s wrong.
I also accept recommendations for cleaner code.
What is the value of
$subtotal
when you’re not in the Else?– Woss
In the code I posted the subtotal is 08:00 so far okay, but you need to put a contributor input in arrears, as for example his arrival at the company at 12:10, so the subtotal will be 07:50 there that does not enter into Else!
– Rafael Souza Calearo
Please see how to make a [mcve], it is no use to open a question pointing out an error and give an example that works as expected. It’s like going to the doctor when you’re healthy and saying, "I’m fine now, but make the appointment so when I’m sick I already know what I have".
– Woss
My guess is it should be
if ($subtotal >= strtotime("08:00"))
, for$subtotal
is already the return ofstrtotime
, then it doesn’t make sense to pass it back to the same function (I did some tests here and saw that PHP converts its value to string and does very strange things, debug and see the value returned bystrtotime($subtotal)
). Anyway, PHP doesn’t have a decent native way of working with durations (or dates, if you ask me :-D), but the way it’s done, I think it’s getting too complicated...– hkotsubo
Dates and durations are different things (a date/time is a specific moment, a duration is a amount of time):
strtotime
andgmdate
work well with dates, but not with durations (if passing 24 hours gives problem, for smaller values "works" by coincidence). Take a look at here, here, here and here to better understand and have some alternatives on how to make these calculations.– hkotsubo
Thanks @Woss for the collaboration, but it was in the rush, I want to adapt better to my published questions!
– Rafael Souza Calearo