-2
I’m having a problem with the time balance calculation of my point system. Basically, I need to know how many hours an employee has, comparing the time he worked in the month with the time he was expected to work during that period.
In my code I can calculate quietly, even if the balance is negative, but at some point it gets lost and I could not understand yet, could help me?
//Teste funcionando
//$horasTrabalhadas = '140:30:00';
//$horasPrevistas = '141:30:00';
//Saldo de horas: -01:00:00
//Teste com erro
$horasTrabalhadas = '141:50:00';
$horasPrevistas = '144:00:00';
// Resultado esperado: 02:10 // Resultado que está aparecendo: 03:50
echo "Saldo de horas: " . calculaTempo($horasPrevistas, $horasTrabalhadas);
function calculaTempo($hora_inicial, $hora_final) {
$i = 1;
$tempo_total='';
$tempos = array($hora_final, $hora_inicial);
foreach($tempos as $tempo) {
$segundos = 0;
list($h, $m, $s) = explode(':', $tempo);
$segundos += $h * 3600;
$segundos += $m * 60;
$segundos += $s;
$tempo_total[$i] = $segundos;
$i++;
}
$segundos = $tempo_total[1] - $tempo_total[2];
$horas = floor($segundos / 3600);
$segundos -= $horas * 3600;
$minutos = str_pad((floor($segundos / 60)), 2, '0', STR_PAD_LEFT);
$segundos -= $minutos * 60;
$segundos = str_pad($segundos, 2, '0', STR_PAD_LEFT);
return "$horas:$minutos:$segundos";
}
From now on I thank you all. :)
See help: https://answall.com/a/388495/112052
– hkotsubo
Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To understand what kind of question serves the site and, consequently, avoid closures and negativities worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese.
– Bacco