-2
I need to subtract the hours and I don’t know how to do it. The variables are over 24hr.
$hora1 = "78:00";
$hora2 = "22:00";
$tempo = $hora1 - $hora2;
resultado 56:00
How to subtract that?
-2
I need to subtract the hours and I don’t know how to do it. The variables are over 24hr.
$hora1 = "78:00";
$hora2 = "22:00";
$tempo = $hora1 - $hora2;
resultado 56:00
How to subtract that?
4
Don’t confuse timetable with amount of hours. What you have are amounts of hours and minutes, separated by two points, which will induce you to think it’s a time. And to subtract quantities, you only have two numerical values.
The easiest is to convert the two quantities to one, of total minutes, subtract between them and after converting back to hours and minutes; this will avoid the problem of subtracting, for example 10:00 and 9:30 getting 1 hour and -30 minutes, which does not make much sense. Thus:
list($horas, $minutos) = explode(':', '78:00');
$minutosTotais1 = $horas * 60 + $minutos;
list($horas, $minutos) = explode(':', '22:00');
$minutosTotais2 = $horas * 60 + $minutos;
$minutosDiferenca = $minutosTotais1 - $minutosTotais2;
$horas = intdiv($minutosDiferenca, 60);
$minutos = $minutosDiferenca % 60;
echo "Diferença de {$horas}:{$minutos}", PHP_EOL;
The exit would be Diferença de 56:0
. You can force the output to be two zeros, 56:00
, through function str_pad
.
thanks, saved my day.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
What is the desired result?
– Woss
edited the question. @Andersoncarloswoss
– smourao
Would you like the result to be what value? @smourao
– RXSD
the value is variant, is according to what is registered in the table, type somo all entries and subtracts all exits and returns me a value (26:00hrs), but I did it directly in sql. With php I’m not getting it. @Andréfilipe
– smourao
@the shape of the hour will always be the same?
– Roberto de Campos