Reputation: 1853
How can I get the total hour from start time to end time.
$start_time = '11:00:00 PM'; // as of 07/08/2013
$end_time = '01:00:00 AM'; // as of 08/08/2013
Then output should be:
$total = '02:00:00'; // 12 hour format
Upvotes: 1
Views: 3684
Reputation: 1
If you want to find out the total time and you only have time values go for this. //24 hour format
for ($i = 0; $i < count($array); $i++) { //array contains your sorted times like [H:i]
$a = $array[$i]. " " .date("Y/m/d");
$array[$i] = $a;
}
$total=0;
for ($i = count($array); $i>1; $i--){
if ($array[$i-1] < $array[$i-2]){
$array[$i-1] = strtotime("+1 day", strtotime($array[$i-1]));
$z = "a";
}
$a = strtotime($array[$i-1]);
$b = strtotime($array[$i-2]);
$diff = $a - $b;
$hours = ceil($diff / (24*60));
$total += date("H", $hours);
}
if ($z == "a"){
$total += date("H", ceil(strtotime($array[0]) / (24*60)));
}
Upvotes: 0
Reputation: 12037
You can convert the date strings to time values, then subtract to get the difference in seconds between the two, then simply divide by 3600 to get the difference in hours:
$t1 = strtotime('2013-08-07 23:00:00');
$t2 = strtotime('2013-08-08 01:00:00');
$differenceInSeconds = $t2 - $t1;
$differenceInHours = $differenceInSeconds / 3600;
Upvotes: 2