How to calculate the difference between two dates using PHP.

In this guide, we will show you how to calculate the difference between two dates in PHP.

This is useful if you need to figure out how much time has passed between two events.

How to calculate the difference between two dates using PHP.

Let’s say, for example, that we have two date strings:

  1. 2013-03-01 19:12:45
  2. 2014-03-01 06:37:04

As you can see, the second date is about one year later than the first one.

However, what if we want to calculate the exact number of seconds that have passed between date one and date two?

//Our dates
$date1 = "2013-03-01 19:12:45";
$date2 = "2014-03-01 06:37:04";

//Convert them to timestamps.
$date1Timestamp = strtotime($date1);
$date2Timestamp = strtotime($date2);

//Calculate the difference.
$difference = $date2Timestamp - $date1Timestamp;

echo $difference;

If you run the code above, you will see that the answer is 31490659.

In other words, 31,490,659 seconds passed between “2013-03-01 19:12:45” and “2014-03-01 06:37:04”.

A basic explanation of what we did:

  1. We convert both of our date strings into a unix timestamp by using the PHP function strtotime. A unix timestamp represents the number of seconds that have passed since 00:00 on the 1st of January, 1971. After converting them into seconds, we can do some basic subtraction to work out the difference.
  2. We then subtract the older date from the newer date.

Remember: The older a date is, the smaller its timestamp will be.

If you run the following code, you will see that the 2014 date has a much larger unix timestamp:

$olderDate = "1991-01-02";
$newerDate = "2014-06-07";

echo $olderDate . " is " . strtotime($olderDate), "<br>";
echo $newerDate . " is " . strtotime($newerDate);

As you can see, “2014-06-07” has a larger timestamp.

This is because more seconds have passed since January 1st, 1971 and June 7th, 2014.

Difference between now and then.

In certain cases, you might need to calculate how many seconds have passed since a given date.

In other words, how much time has passed between “now” and “then”?

//Our dates and times.
$then = "2011-02-02 08:00:00";
$now = time();

//convert $then into a timestamp.
$thenTimestamp = strtotime($then);

//Get the difference in seconds.
$difference = $now - $thenTimestamp;

echo $difference;

An explanation of this code:

  • We retrieve the current timestamp by using the time function.
  • We then subtract the older timestamp from our current timestamp.

If you refresh the page a couple of times, you will see that the difference in seconds continues to grow.

Minutes and Days.

In a lot of cases, you will want the number of days or minutes that have passed.

This is because seconds are kind of useless to an end user.

To calculate the number of days that have passed, you can do the following:

//Our "then" date.
$then = "2009-02-04";

//Convert it into a timestamp.
$then = strtotime($then);

//Get the current timestamp.
$now = time();

//Calculate the difference.
$difference = $now - $then;

//Convert seconds into days.
$days = floor($difference / (60*60*24) );

echo $days;

As you can see, we were able to convert seconds into days by using some basic math:

  1. There are 60 seconds in a minute.
  2. There are 60 minutes in an hour.
  3. There are 24 hours in a day.

If you multiply the figures above, it will give you 86400, which is the total number of seconds in a day.

If you divide the difference in seconds between our two dates by 86,400, you will get the total number of days that have passed.

You can then use the floor function in order to round the result down. In other words, 2.9 days should become 2 days.

To get the number of minutes, you can simply divide the difference in seconds by 60:

//Our "then" date.
$then = "2009-02-04";

//Convert it into a timestamp.
$then = strtotime($then);

//Get the current timestamp.
$now = time();

//Calculate the difference.
$difference = $now - $then;

//Convert seconds into minutes.
$minutes = floor($difference / 60);

echo $minutes;

As you can see, most of these calculations are actually pretty simple.

Using DateTime to calculate the difference.

Once you understand the basics of dealing with dates and timestamps in PHP, you can use the DateTime object.

Using DateTime is a better solution simply because it’s cleaner and it provides you with an OO interface.

For example, if we want to get the difference in days using the DateTime object, we can do the following:

$date1 = new DateTime("2011-07-06"); //then
$date2 = new DateTime(); //now

$diff = $date2->diff($date1)->format("%a");

echo $diff;

The code above is much more concise than the previous examples.

In our final example, we will convert the difference into years, months, days, hours, and minutes:

$then = '2005-09-01 09:02:23';
$then = new DateTime($then);

$now = new DateTime();

$sinceThen = $then->diff($now);

//Combined
echo $sinceThen->y.' years have passed.<br>';
echo $sinceThen->m.' months have passed.<br>';
echo $sinceThen->d.' days have passed.<br>';
echo $sinceThen->h.' hours have passed.<br>';
echo $sinceThen->i.' minutes have passed.<br>';

As you can see, the DateTime object is a pretty powerful tool once you figure out how to use it.