Calculate difference between hours in php [duplicate]

1

I'm trying to show how many hours an employee has worked, but the forms I've tried have incorrect values. They follow the same ones 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 one returned 21:00:06 for the above exemplified hours:

date('H:i:s',($horario1 - $horario2)));
    
asked by anonymous 06.05.2016 / 21:11

1 answer

6

I did it that way and it worked correctly:

//Calcula o tempo de upload
$entrada = $horario1;
$saida = $horario2;
$hora1 = explode(":",$entrada);
$hora2 = explode(":",$saida);
$acumulador1 = ($hora1[0] * 3600) + ($hora1[1] * 60) + $hora1[2];
$acumulador2 = ($hora2[0] * 3600) + ($hora2[1] * 60) + $hora2[2];
$resultado = $acumulador2 - $acumulador1;
$hora_ponto = floor($resultado / 3600);
$resultado = $resultado - ($hora_ponto * 3600);
$min_ponto = floor($resultado / 60);
$resultado = $resultado - ($min_ponto * 60);
$secs_ponto = $resultado;
//Grava na variável resultado final
$tempo = $hora_ponto.":".$min_ponto.":".$secs_ponto;
echo $tempo;
    
06.05.2016 / 21:28