2
I am trying to show how many hours an employee has worked, however the ways I tried presented incorrect values. Follow the same below:
This returns the difference with 3 hours and 2 minutes less:
$horario1 = '16:05:00';
$horario2 = '10:16:53';
date('H:i:s',(strtotime($horario1) - strtotime($horario2))));
And this returned 21:00:06 for the hours above exemplified:
date('H:i:s',($horario1 - $horario2)));
It worked correctly. Could you explain to me why of multiplication and division by 3600 and 60?
– Marcelo Augusto
3600 turns seconds into hours and vice versa. 60 minutes into seconds.
– Diego
I think it’s cool as didactic, but in practice PHP already has simpler functions and ready to solve the problem.
strtotime( '1970-01-01 '.$horario1 )
already returns the value in seconds.– Bacco
Now, if it’s a case where the time, minute and second are already separated, definitely I also think better to use mathematics than the textual interpretation of
strtotime
. However, +1– Bacco
Another interesting way to get the total seconds having hours and minutes apart is this:
mktime ($hora,$minuto,$segundo,1,1,1970)
– Bacco
@Bacco, you’re right. But I broke my head a lot and it was this mathematical solution that best solved my case. Thanks for the considerations.
– Diego