I did not understand well the use of the negative hours, in my view it would be better to have the number of hours you had access and deduct from the daily, weekly or monthly hours.
Seems like that is answer in the Soen solves your problem
$start = DateTime::createFromFormat('H:i:s', '11:30:00');
$start->add(new DateInterval('PT8H30M'));
$end = DateTime::createFromFormat('H:i:s', '19:30:00');
$diff = $start->diff($end);
echo $diff->format('%r%H:%I');
I didn’t test it, but apparently you add 8 and a half hours and deduce from the final result.
However if you want something simpler, you can use Unix-time, something like (using the function of this answer How to get the format in hours when it exceeds 24?):
//Transforma as horas em "inteiro"
function toUnixTime($total) {
$negativo = false;
if (strpos($total, '-') === 0) {
$negativo = true;
$total = str_replace('-', '', $total);
}
list($horas, $minutos, $segundos) = explode(':', $total);
$ut = mktime($horas, $minutos, $segundos);
if ($negativo) {
return -$ut;
}
return $ut;
}
//Gera horarios acima de 24 horas (para calculo total)
function getFullHour($input) {
$seconds = intval($input);
$resp = NULL;//Em caso de receber um valor não suportado retorna nulo
if (is_int($seconds)) {
$hours = floor($seconds / 3600);
$mins = floor(($seconds - ($hours * 3600)) / 60);
$secs = floor($seconds % 60);
$resp = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
}
return $resp;
}
$dia1 = toUnixTime('-05:00:00');
$dia2 = toUnixTime('08:00:00');
//Compara os dois horarios
$calculo = $dia1 + $dia2;
echo getFullHour($calculo);
Negative hours? How does this work, would it be hours that the user should, like he didn’t go to work? Wouldn’t it be better to just calculate the hours accessed and deduct from the weekly hours for example?
– Guilherme Nascimento
Good afternoon, the answer solved your problem?
– Guilherme Nascimento