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.

I'm using the code below:

$time1 = '08:00:00';
$time2 = '09:30:00';
$difference = $time2 - $time1;
echo $difference;

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


answered Feb 13, 2018 at 13:48

swathi_sriswathi_sri

4074 silver badges8 bronze badges

4

Problem:

You have to dates and you want to calculate hours between those two dates.

Solution:

In the following you’ll see two methods to finds out hour difference between two dates

Method 1: Using strtotime[] function

Using strtotime[] function to find out hours is two steps process

  1. Convert each date to its equivalent timestamp [as you know, timestamp is number of seconds since January 1 1970 00:00:00 UTC]
  2. Divide the timestamp by [60*60] to get the number of hours.

See the following example how it works-

[wpdm_file id=119]

Output:
Difference between two dates is 25 hour[s]

How it works:

Line 3:
We convert the first date to its timestamp.Line 4:
We convert the second date to its timestamp.Line 5:
As the difference between two dates might be negative, we use absolute function, abs[], to get the value only. Then, we divided it by 60*60 to get the hours.

Method 2: Using diff[] method of DateTIme class from SPL

Calculating hours between two dates is 3 steps process-

  1. Convert each date to the DateTime object
  2. Calculate the day interval between two dates as object using diff[] method.
  3. Convert the Day interval object to its equivalent number of hours using format[] method.

[wpdm_file id=120]

Output:
Difference between two dates is 25 hour[s]

How it works:

Line 5-6:
We convert two dates to their respective DateTime objects.Line 7:
The diff[] method subtracts $datetimeObj1 from $datetimeObj2 and returns as an object.Line 9-11:
Here, we check if the interval between two dates is more than 1 day. If so, we convert the day to hours by multiplying it by 24. Here, format[‘%a’] returns total number of days.

Line 12-14:
The hour difference might be less than 1 day. In that case we directly convert the interval object to hours. Here, format[‘%a’] returns total number of hours.

Line 16:
As the time difference between two dates might be day difference and hour difference between those, so we added the both of those to get the real hour difference.

Post navigation

How 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];.

Chủ Đề