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
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
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' ;
?>
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.