0

Hi,
How to calculate hours in PHP in when time is coming in Am - PM format ?

for example,
1] start time: 9AM
end time: 12PM

2] start time: 9PM
end time: 1AM

3] start time: 12.30PM
end time: 1.25PM

any formula please

Edited by Priti_P

2
Contributors
2
Replies
6
Views
3 Years
Discussion Span
Last Post by GliderPilot
0

hello,
I got the solution
it is as below:

<?php
$start_time = '11:30';
$end_time = '1:50';
$start = explode(':', $start_time);
$end = explode(':', $end_time);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);
$total_minutes=$end[1] -$start[1];
if ($total_hours < 0) {
 $total_hours += 12;
}
echo $total_hours . ' hours:'. $total_minutes. 'minutes' ;

?>
0

Use PHP's strtotime to turn the am / pm into a timestamp and substract the diff. Than you can output the diff.

$diff = strtotime("1:30 pm") - strtotime("11:30 am");

$hours = date('H', $diff);

echo $hours . " Hours."; // 2 Hours

Edited by GliderPilot

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.