DateTime() and DateInterval() are what you're looking for:
$date1 = new DateTime($info["Hora_final"]);
$date2 = new DateTime($info["Hora_inicial"]);
$diff = $date1->diff($date2);
echo $diff->format("%h hours, %i minutes");
سؤال
I'm traying to make a mathematical operation with php I have 2 variables,
$a2 = $info["Hora_final"]; //END TIME Example-> 15:30:00
$a1 = $info["Hora_inicial"];//FIRST TIME Example-> 10:00:20
But i want this operation (-) minus.
$res = $a2 - $a1 ;
echo $res;
//output 5,30 Hours difference for example.
I try with this function but, is no like i want.
$a2 = $info["Hora_final"];
$a1 = $info["Hora_inicial"];
$h2h = date('H', strtotime($a2));
$h2m = date('i', strtotime($a2));
$h2s = date('s', strtotime($a2));
$hora2 =$h2h." hour ". $h2m ." min ".$h2s ." second";
$horas_sumadas= $a1." - ". $hora2;
$text=date('H:i:s', strtotime($horas_sumadas)) ;
Thanks for you help ;)
المحلول
DateTime() and DateInterval() are what you're looking for:
$date1 = new DateTime($info["Hora_final"]);
$date2 = new DateTime($info["Hora_inicial"]);
$diff = $date1->diff($date2);
echo $diff->format("%h hours, %i minutes");
نصائح أخرى
The PHP DateTime class is perfect for this. http://www.php.net/manual/en/class.datetime.php
Create 2 DateTime objects then use the diff
method:
http://www.php.net/manual/en/datetime.diff.php
(Requires PHP 5.2 or higher)