How do you calculate the difference between two dates?

Question:
How do you calculate the difference between two dates? I.e. the number of days to Christmas?

Number of days

Convert both dates to a UNIX timestamp and substract one from the other. The result will be the number of seconds until that date (in our case until Christmas). Then you can convert seconds to days by dividing by 86400 (60 seconds in a minute multiplied by 60 minutes in an hour multplied by 24 hours in a day = 86400).

Unless the script is run at exactly midnight then you'll have a remainder over when you divide by 86400. This represents the part of day still remaining and in the first example I demonstrate a rounding technique to display a whole number for the days. In the second example I demonstrate a more advanced technique where we can display the days, hours and minutes remaining until Christmas.

$Christmas = mktime(0, 0, 0, 12, 25, date('Y'));
$today = mktime();
 
$seconds_to_christmas = $Christmas - $today;
 
$days_to_christmas = $seconds_to_christmas / 86400;
$days_to_christmas = number_format($days_to_christmas, 0);
 
echo '<p>Number of Days till Christmas is: ' . $days_to_christmas . '</p>';

Including hours and minutes

If you're not happy just providing the number of days till Christmas we can also provide more detailed information such as the number of hours, minutes, and seconds as well. I.e. Number of Days till Christmas is: 10 days 3 hours and 32 minutes.

To do that we need to subtract the WHOLE number of days as represented in seconds from the seconds_to_christmas variable. This leaves us with the remainder of the part of a day. We can divide that amount by 3600 (60 seconds in a minute multiplied by 60 minutes in an hour = 3600) and this will give us the number of hours.

...
$days_to_christmas = $seconds_to_christmas / 86400;
$days_to_christmas = floor($days_to_christmas);
 
$temp_remainder = $seconds_to_christmas - ($days_to_christmas * 86400);
$hours = floor($temp_remainder / 3600);
 
$temp_remainder = $temp_remainder - ($hours * 3600);
$minutes = round($temp_remainder / 60, 0);
 
echo '<p>There is ' . $days_to_christmas . ' days and ' .
      $hours . ' hours and ' . $minutes .
      ' minutes until Christmas</p>';

This example diverges slightly from the first one. Whereas before I used number_format to produce an integer representation for days, this function also rounds the result to the nearest integer. This is not suitable for the second example because we need the fractional part to calculate the number of hours and minutes.

The fractional part of this equation

$days_to_christmas = $seconds_to_christmas / 86400;

represents the part that is not a whole day. I.e a part day that we can convert into hours and minutes. To do that we subtract the whole day (represented in seconds) from the $seconds_to_christmas variable (as shown below)

$temp_remainder = $seconds_to_christmas - ($days_to_christmas * 86400);

Remember that $days_to_christmas is represented in days, therefore it must be multiplied by 86400 to convert it back to seconds.

The hours are then calculated by dividing this remainder by 3600 (60 seconds in an minute multiplied by 60 minutes in an hour = 3600). Once again only the whole integer amount is used and this is done using the floor function.

The same process is applied for the minutes (and can be done for seconds as well).


Additional Notes:
number_format and round are NOT interchangeable. number_format will format the variable to the number of decimal places that you specify. It will also add the thousands separator and decimal point for you, but both of these are optional parameters in the function call. round on the other hand simply rounds the number to the nearest integer.

Comments

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.