# How Can I Calculate the Difference Between Two Dates in PHP?

Calculating the difference between two dates is not as easy as it sounds. There are a lot of things that can go wrong if you are not careful. For example, you have to take both leap years and daylight saving time into consideration. Simply converting two dates to a timestamp and subtracting the values is not reliable in all cases.

Fortunately, the PHP DateTime class takes care of all this for us. In this tutorial, we will use the different methods from this class to get the difference between two dates.

## Get the Difference Between Two Dates Using DateTime::diff()

The `DateTime`

class has a diff() method which calculates the difference between two `DateTime`

objects. It returns a `DateInterval`

object which contains information like the number of years, months and days etc. between two given dates.

Once you have the `DateInterval`

, you can use the format() method to format it according to your needs. This method accepts different characters as part of its string parameter. Just remember that all these characters must be prefixed by a percent sign (%).

Here is a list of all the format characters and their usage:

**%**— This will output a literal % character.**Y**— This will output years numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 15.**y**— This will output years numerically without any leading zeroes. Examples are 1, 3, 15.**M**— This will output months numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 05, 28.**m**— This will output months numerically without any leading zeroes. Examples are 1, 3, 12.**D**— This will output days numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 09, 29.**d**— This will output days numerically without any leading zeroes. Examples are 1, 9, 31.**a**— This will output the total number of days computed by`DateTime::diff()`

or (unknown) otherwise.**H**— This will output hours numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 23.**h**— This will output hours numerically without any leading zeroes. Examples are 1, 23.**I**— This will output minutes numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 59.**i**— This will output minutes numerically without any leading zeroes. Examples are 1, 3, 59.**S**— This will output seconds numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 57.**s**— This will output seconds numerically without any leading zeroes. Examples are 1, 3, 57.**F**— This will output microseconds numerically with at least 6 digits. Single digits will be followed by leading zero. Examples are 007701, 052738, 428291.**f**— This will output microseconds numerically with at least 6 digits. Single digits will be followed by leading zero. Examples are 7701, 52738, 428291.**R**— This will output “-” when negative and “+” when positive.**r**— This will output “-” when negative and nothing when positive.

In the following example, we have calculated the number of years, months and days between two dates.

PHP

```
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);
// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);
// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
```

The `DateTime`

class takes care of everything for you like different number of days in different months, leap years and daylight saving time etc.

### Get the Number of Days Between Two Dates

Once you have computed the difference between two dates using the `diff()`

method, getting the number of days between them is easy. You just have to pass the `%a`

character sequence to the `format()`

method. It is important the remember that the days returned by `%D`

or `%d`

and `%a`

are different.

The `%D`

and `%d`

character sequences return the number of days between two dates after subtracting the number of months and years. Their value will never be greater than 31. On the other hand, the `%a`

character sequence returns the total number of days that have passed between two dates. Here is an example:

PHP

```
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);
// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');
// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
```

## Calculate the Number of Hours Between Two Dates

While the format method has the `%a`

character sequence to get the total number of days that have passed between two dates, there is no such character to get the total number of hours. However, we can do that calculation ourselves.

So far, we have only created `DateTime`

objects where we did not pass any information about the time of the day in the string. This meant that value returned for `%H`

(hours), `%I`

(minutes) and `%S`

(seconds) would always be zero. Since we want to learn how to calculate the number of hours between two dates, we will pass strings with different times and/or dates this time. The good thing about the `diff()`

method of the `DateTime()`

class is that it also takes care of the difference in timezones.

In the following example, the number of hours returned will be different because of different time zones.

PHP

```
$first_date = new DateTime('2018-01-09T11:56:59+00:00');
$second_date = new DateTime('2018-01-09T17:19:29+00:00');
$interval = $first_date->diff($second_date);
// Output — (+) 05 hours 22 minutes and 30 seconds.
echo $interval->format('(%R) %H hours %I minutes and %S seconds.');
$first_date = new DateTime('2018-01-09T11:56:59+00:00');
$second_date = new DateTime('2018-01-09T17:19:29-08:00');
$interval = $first_date->diff($second_date);
// Output — (+) 13 hours 22 minutes and 30 seconds.
echo $interval->format('(%R) %H hours %I minutes and %S seconds.');
$first_date = new DateTime('2018-01-09T11:56:59+00:00');
$second_date = new DateTime('2018-01-09T17:19:29+08:00');
$interval = $first_date->diff($second_date);
// Output — (-) 02 hours 37 minutes and 30 seconds.
echo $interval->format('(%R) %H hours %I minutes and %S seconds.');
```

The (+) sign implies that the second date is in future with respect to the first date. The (-) sign implies that the second date is in the past with respect to the first date.

When the difference between two dates is less than a day, the value returned by `%H`

will accurately give you the difference between them in hours. When the difference between two dates is more than a day like a week, some months or a couple of years etc, you can just multiply the number of days returned by `%a`

with 24 and then add that to the number of hours returned by `%H`

. Again, you don’t have to worry about leap years etc. because the `DateTime`

class takes care of all that for you.

PHP

```
$first_date = new DateTime('2018-01-19T11:56:59+00:00');
$second_date = new DateTime('2018-01-09T17:19:29+08:00');
$interval = $first_date->diff($second_date);
$days_passed = $interval->format('%a');
$hours_diff = $interval->format('%H');
// Output — Total Days Passed: 10
echo 'Total Days Passed: '.$days_passed;
// Output — Hour Difference in Two Times: 02
echo 'Hour Difference in Two Times: '.$hours_diff;
// Output — Total Hours Passed: 242
echo 'Total Hours Passed: '.($days_passed*24 + $hours_diff);
```

You might have noticed that the second date in above example is in the past relative the the first date. The time in second date becomes 9:19:29 after accounting for the difference in timezone. That explain the 02 value returned by `%H`

.

In the following example, I have switched the dates. The timezone difference is still the same. Therefore, the time for second date stays 9:19:29. Since this is less than 11:56:59 in the first date, the number of days passed changes from 10 to 9. This is because the days are counted only up to 2018-01-18 11:56:59 as 2018-01-19 11:56:59 would be more than the second date (after accounting for timezone differences). Now, hours passed from 2018-01-18 11:56:59 to 2018-01-19 9:19:29 are 21. This takes our total number of hours to 237.

As you can see, the `DateTime`

class handles all the complications for you and gives you the correct result without any problem.

PHP

```
$first_date = new DateTime('2018-01-09T11:56:59+00:00');
$second_date = new DateTime('2018-01-19T17:19:29+08:00');
$interval = $first_date->diff($second_date);
$days_passed = $interval->format('%a');
$hours_passed = $interval->format('%H');
// Output — Total Days Passed: 9
echo 'Total Days Passed: '.$days_passed;
// Output — Hour Difference in Two Times: 21
echo 'Hour Difference in Two Times: '.$hours_passed;
// Output — Total Hours Passed: 237
echo 'Total Hours Passed: '.($days_passed*24 + $hours_passed);
```

## Calculate the Number of Minutes Between Two Dates

The number of minutes returned by the `%I`

character sequence in the `format()`

method is equal to the number of minutes left after the `diff()`

function had already accounted for all the days and hours that have passed. If you want to get the total number of minutes that have passed between two dates, you will have to do some calculations yourself.

We first have to convert the number of days returned by `diff()`

into hours by multiplying it with 24 and after that convert the total number of hours to minutes by multiplying the result with 60. The `DateTime`

class automatically takes care of leap years and day time savings for you so simple multiplication gives accurate results.

PHP

```
$first_date = new DateTime('2018-01-19T11:56:59+00:00');
$second_date = new DateTime('1993-10-09T17:34:22+08:00');
$interval = $first_date->diff($second_date);
$days_passed = $interval->format('%a');
$hours_diff = $interval->format('%H');
$minutes_diff = $interval->format('%I');
$total_minutes = (($days_passed*24 + $hours_diff) * 60 + $minutes_diff);
// Output — Total Days Passed: 8868
echo 'Total Days Passed: '.$days_passed;
// Output — Hour Difference in Two Times: 02
echo 'Hour Difference in Two Times: '.$hours_diff;
// Output — Minute Difference in Two Times: 22
echo 'Minute Difference in Two Times: '.$minutes_diff;
// Output — Total Minutes Passed: 12770062
echo 'Total Minutes Passed: '.$total_minutes;
```

One thing that I would like to repeat is that you have to use `%I`

to get the minutes. It is common to use `%M`

in a hurry but remember that `%M`

is reserved for months.

## Calculate the Number of Seconds Between Two Dates

Just like minutes, the number of seconds returned by the `%S`

character sequence in the `format()`

method are calculated after the `diff()`

method has already account for the number of days, hours and minutes that have already passed. However, calculating the total number of seconds that have passed between two dates is easy once we have the days, hours and minutes. All you have to do is convert them to seconds.

We begin by converting the days and hours to minutes like we did in the previous section. After that, we can just multiply the result by 60 and add the number of seconds returned by the `%S`

character sequence.

PHP

```
$first_date = new DateTime('2018-01-19T00:00:00+00:00');
$second_date = new DateTime('1994-02-25T17:34:22+05:30');
$interval = $first_date->diff($second_date);
$days_passed = $interval->format('%a');
$hours_diff = $interval->format('%H');
$minutes_diff = $interval->format('%I');
$seconds_diff = $interval->format('%S');
$total_minutes = (($days_passed*24 + $hours_diff) * 60 + $minutes_diff);
$total_seconds = $total_minutes*60 + $seconds_diff;
// Output — Total Days Passed: 8728
echo 'Total Days Passed: '.$days_passed;
// Output — Hour Difference in Two Times: 11
echo 'Hour Difference in Two Times: '.$hours_diff;
// Output — Minute Difference in Two Times: 55
echo 'Minute Difference in Two Times: '.$minutes_diff;
// Output — Seconds Difference in Two Times: 38
echo 'Seconds Difference in Two Times: '.$seconds_diff;
// Output — Total Seconds Passed: 754142138
echo 'Total Seconds Passed: '.$total_seconds;
```

## Quick Summary

Let’s recap everything that we have covered in this tutorial.

- You can calculate the difference between two dates using the
`diff()`

method of the`DateTime`

class. After that, you can echo the result in a specific format using the`format()`

method. - If you want to calculate the total number of hours between two dates, you can simply multiply the number of days returned by
`diff()`

with 24 and then add the result to the number of hours returned by`diff()`

. - Similarly, you can calculate the total number of minutes between two dates by first calculating the total hours passed and multiplying that by 60. After that, just add the result to the number of minutes returned by
`diff()`

. - The same technique can be applied to calculate the number of seconds between two dates. Just calculate the number of minutes first and convert them to seconds by multiplying with 60. After that you just have to add the result to the number of seconds returned by
`diff()`

.

Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to calculate the number of days, hours, minutes or seconds between two dates in PHP.

**Rate this post — **