Php calculate hours between two times
I need to calculate the difference between two times in hours. For example, the difference between 08:00:00 and 09:30:00 would be 1.5 hours. Show I'm using the code below:
Instead of getting 1.5 back as I'd expect, I'm getting 1. I'm sure this is a time formatting issue that someone can easily advise me on. Hopefully... :) asked Apr 16, 2015 at 3:33
You can try my code
Note: The code above will round to minutes. answered Apr 16, 2015 at 3:38
0
answered Feb 13, 2018 at 13:48
swathi_sriswathi_sri 4074 silver badges8 bronze badges 4 You have to dates and you want to calculate hours between those two dates. In the following you’ll see two methods to finds out hour difference between two dates Using strtotime() function to find out hours is two steps
process See the following example how it works- [wpdm_file id=119] Output: How it works: Line 3: Method 2: Using diff() method of DateTIme class from SPLCalculating hours between two dates is 3 steps process-
diff($datetimeObj2); if($interval->format('%a') > 0){ $hour1 = $interval->format('%a')*24; } if($interval->format('%h') > 0){ $hour2 = $interval->format('%h'); } echo "Difference between two dates is " . ($hour1 + $hour2) . " hours."; ?> [wpdm_file id=120] Output: How it works: Line 5-6: Line 12-14: Line 16: Post navigationHow can I calculate hours between two dates in PHP?You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
How can I calculate total hours in PHP?There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format. Example 1: This example reads the values from the array and converts it into the time format.
How can I get minutes between two dates in PHP?php $start = strtotime('12:01:00'); $end = strtotime('13:16:00'); $mins = ($end - $start) / 60; echo $mins; ?>
How is start time and end time calculated in PHP?“calculate total time from start and end datetime in php” Code Answer. date1 = new DateTime('2006-04-12T12:30:00');. $date2 = new DateTime('2006-04-14T11:30:00');. $diff = $date2->diff($date1);. $hours = $diff->h;. $hours = $hours + ($diff->days*24);. |