Priti_P 0 Junior Poster 10 Years Ago 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 php 0 0 Share Priti_P 0 Junior Poster 10 Years Ago hello, I got the solution it is as below: Copy to Clipboard<?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 0 Share GliderPilot 33 Posting Whiz 10 Years Ago Use PHP's strtotime to turn the am / pm into a timestamp and substract the diff. Than you can output the diff. Copy to Clipboard$diff = strtotime("1:30 pm") - strtotime("11:30 am"); $hours = date('H', $diff); echo $hours . " Hours."; // 2 Hours 0 0 Share Share Facebook Twitter LinkedIn Reply to this topic Be a part of the DaniWeb community We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge. Sign Up — It's Free!
Priti_P 0 Junior Poster 10 Years Ago hello, I got the solution it is as below: Copy to Clipboard<?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 0 Share
GliderPilot 33 Posting Whiz 10 Years Ago Use PHP's strtotime to turn the am / pm into a timestamp and substract the diff. Than you can output the diff. Copy to Clipboard$diff = strtotime("1:30 pm") - strtotime("11:30 am"); $hours = date('H', $diff); echo $hours . " Hours."; // 2 Hours 0 0 Share