3
I have 2 strings that represent times. How to calculate the difference between the two.
$string1 = "12:04:32";
$string2 = "18:07:34";
3
I have 2 strings that represent times. How to calculate the difference between the two.
$string1 = "12:04:32";
$string2 = "18:07:34";
4
You can use the combination of methods diff()
which calculates the difference between the hours and format() to format the time as desired in the class Datetime.
Datetime has existed since PHP 5.2, already methods diff() and createFromFormat() are only available in PHP 5.3
<?php
$hora1 = DateTime::createFromFormat('H:i:s', '12:04:32');
$hora2 = DateTime::createFromFormat('H:i:s', '18:07:34');
echo $hora1->diff($hora2)->format('%H horas %i minutos e %s segundos');
+1 orientation is always preferable to object on account of reuse :)
2
Try the code below:
<?php
$string1 = strtotime("12:04:32");
$string2 = strtotime("18:07:34");
$intervalo = abs($string2 - $string1);
var_dump('Diferença em segundos: ' . $intervalo);
$minutos = round($intervalo / 60, 2);
var_dump('Diferença em minutos: ' . $minutos);
$horas = round($minutos / 60, 2);
var_dump('Diferença em horas: ' . $horas);
?>
2
You can use the object DateTime()
:
<?php
$string1 = "12:04:32";
$string2 = "18:07:34";
list($h1,$m1,$s1) = explode(':',$string1);
list($h2,$m2,$s2) = explode(':',$string2);
$dateTimeOne = new DateTime();
$dateTimeOne->setTime($h1, $m1, $s1);
$dateTimeTwo = new DateTime();
$dateTimeTwo->setTime($h2, $m2, $s2);
$interval = $dateTimeOne->diff($dateTimeTwo);
echo $interval->format('%H horas %i minutos e %s segundos');
+1 Although I didn’t like the explode
, I agree with the use of setTime
makes the whole thing more interesting. And also differentiates from other answers, giving more possibilities of execution :)
If you don’t want to use, can do something like this
0
You can convert hours and minutes to seconds to make the difference easier to calculate.
$horaA = "01:30:00"; // HORA INICIAL
$horaB = "20:23:12"; // HORA FINAL
// Função de calculo. Se tiver mais de uma hora para calcular
// torne-a uma variável e adicione um parâmetro na função
function calculaTempo($hora_inicial, $hora_final){
$i = 1; // Para facilitar a lógica
$tempo_total;
$tempos = array($hora_final, $hora_inicial);
foreach($tempos as $tempo) {
$segundos = 0;
// Cria uma lista com os valores de hora, minuto e segundo
list($h, $m, $s) = explode(':', $tempo);
// Passagem dos valores para segundos
$segundos += $h * 3600;
$segundos += $m * 60;
$segundos += $s;
$tempo_total[$i] = $segundos;
$i++;
}
// Calculo da diferença do tempo em segundos
// $horaA $horaB $horaC...
$segundos = $tempo_total[1] - $tempo_total[2];
// Valor das horas
$horas = floor($segundos / 3600);
$segundos -= $horas * 3600;
// Valor dos minutos
$minutos = str_pad((floor($segundos / 60)), 2, '0', STR_PAD_LEFT);
$segundos -= $minutos * 60;
// Valor dos segundos
$segundos = str_pad($segundos, 2, '0', STR_PAD_LEFT);
// RESULTADO
return "$horas:$minutos:$segundos";
}
$horario=calculaTempo($horaA, $horaB);
-3
$timeString = date('H:i:s',strtotime($time))
-5
Using
strtotome($string_da_dora)
Browser other questions tagged php datetime
You are not signed in. Login or sign up in order to post.
you can use the
diff->
if you use object orientation.– RFL
Just to complement the answers, I recommend reading http://answall.com/questions/51821/howto get o-formato-formato-horas-quando-esta-ultrapassa-24
– Wallace Maxters