Calculate the difference between two dates in PHP.

This is a simple guide on how to calculate the difference between two dates in PHP. Be sure to try out the examples below if you’re new to the topic.

Our dates.

Let us say, for example, that we have two date strings and that they are formatted like so:

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

As you can see, there’s about a year in the difference between them.

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

<?php

//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’ll see that the answer is: 31490659. i.e. 31490659 seconds passed between “2013-03-01 19:12:45” and “2014-03-01 06:37:04”.

A basic drill down of what I did:

  1. I converted both of our date strings into a unix timestamp by using the built-in PHP function strtotime. A unix timestamp represents the number of seconds that have passed since 00:00 on the 1st of January, 1971. By converting them into Unix Time, I’ve converted the dates into a format that allows me to do some basic calculations.
  2. I then subtracted the older date from the newer date.

Remember: The older a date is, the smaller its corresponding timestamp will be! Run the following example to see what I mean:

<?php

$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 much bigger timestamp, simply because more seconds have passed since the 1st of January, 1971 and the 7th of June, 2014.

Between then and now.

What if you need to find out how many seconds have passed since a given date?

<?php

//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;

In the code above:

  • I’ve used the function time to retrieve the current timestamp. i.e. Our current Unix Time.
  • I then subtracted the older timestamp from our current timestamp.

If you continue to refresh the page, you’ll see that the difference in seconds will continue to grow.

Minutes & Days…

In many cases, you’ll want the number of days or minutes that have passed. Let’s face it: Seconds are kind of useless to an end user.

To calculate the number of days that have passed:

<?php

//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, I was able to convert seconds into days via some basic math:

  • There are 60 seconds in a minute.
  • There are 60 minutes in an hour.
  • There are 24 hours in a day.

If you multiply the figures above, it will give you 86400, which is total the number of seconds that is in a given day. Divide the difference in seconds between our two dates by 86400 and you’ve got the number of days that have passed.

You can then use the floor function in order to round the result down. i.e. 2.9 days = 2 days.

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

<?php

//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, it’s pretty simple.

OOP

Once you’ve grasped the fundamentals of dealing with dates and timestamps in PHP, you should look into using the DateTime class, 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 class, we can do the following:

<?php

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

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

echo $diff;

Much better.

To transform the difference into a human-readable format that is comprised of years, months, days, hours and minutes:

<?php

$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 class is pretty powerful once you figure out to use it!