PHP Date Difference

Sometimes we need to calculate difference between two dates and times. For example, calculating ageshow time interval. In this tutorial I will show you how to calculate difference between dates and times in PHP.

If you want to get the difference between two dates or time values, first of all you need to get the values of two dates in the same format. The best way is to convert both dates into Unix timestamp format. In this case, the date and time information is stored as an integer so that we can calculate the difference easily. You can get the Unix timestamp in various ways as you can see below:

Code:

<?php

// Get current time

$date1 = time();

// Get the timestamp of 2006 October 20

$date2 = mktime(0,0,0,10,20,2006);

?>

Now as you have the values, so getting the difference is quite easy. However the result will be an integer value, but you probably want to get it in days, hours and minutes. To do so we need to convert back our int value into a usable date format. We know that the difference is in seconds so we can get how many full days it is. To get it we use the following code:

<?php

$dateDiff = $date1 - $date2;

$fullDays = floor($dateDiff/(60*60*24));

echo "Differernce is $fullDays days";

?>

I used the floor function as I want to get only the complete days. Besides this the 60*60*24 represents that a day has 24 hours and each hour has 60 minutes and each minute has 60 seconds.

As next step we need to get how many hours are still present in the remaining seconds. So from the original difference we need to remove the complete days in seconds and from the remaining value we can calculate the full hours similar to as we calculated the full days. Repeating these steps for minutes as well at the end we get the difference between the original dates. The complete calculation looks like this:

<?php

$dateDiff = $date1 - $date2;

$fullDays = floor($dateDiff/(60*60*24));

$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));

$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);

echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes.";

?>

Few more examples:

Example 1

Code:

<?php

$date1 = "1984-07-01";

$date2 = "2011-03-16";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));

$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));

$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

echo "$years years, $months months, $days days";

?>

 

Output:

26 years, 8 months, 24 days

Example 2

Code:

<?php

$date1 = new DateTime("2007-03-24");

$date2 = new DateTime("2009-06-26");

$interval = $date1->diff($date2);

echo "Difference: " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days";

?>

 

Output:

Difference: 2 years, 3 months, 2 days

Example 3

Code:

<?php

$date1 = "2008-11-01 22:45:00";

$date2 = "2009-12-04 13:44:01";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));

$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));

$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));

$minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);

$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));

printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);

?>

 

Output:

1 years, 1 months, 2 days, 14 hours, 59 minuts , 1 seconds