How to Calculate Time Difference With PHP
Sometimes you may need to know the time difference between two dates on your website -- for example, if you have a countdown timer. PHP has several functions that will return dates and times in various forms but they do not allow for much flexibility. By combining these functions with one you create yourself, you can calculate the difference between two dates and times, saving values from seconds to years in separate variables. You can then edit or display these values as desired.
Instructions
-
-
1
Open an HTML file and type the following code in the "<body>" element:
<?php
function timeDiff($first,$last) {
This opens a PHP tag and defines a new function called "timeDiff" that accepts two parameters. These will be in the form of two dates with a "YYYY-MM-DD HH:MM:SS" format, allowing the predefined PHP functions to manipulate them.
-
2
Type the following:
if ($first > $last) {
$tmp = $last;
$last = $first;
$first = $tmp;
}
This block ensures that, regardless of whether the first parameter comes chronologically before or after the second parameter, the function returns a positive time difference.
-
-
3
Type the following code:
$firstTime=strtotime($first);
$lastTime=strtotime($last);
$years = $days = $hours = $minutes = 0;
$seconds=$lastTime-$firstTime;
The first two lines create a pair of variables out of the two date parameters and convert each to a number of seconds elapsed since January 1, 1970 (Unix time) so that the values can be modified. The third line creates four counting variables and initializes them to zero. The last line creates a variable that gets the difference in seconds from the first date to the last date.
-
4
Type the following code:
while ($seconds >= 31536000) {
$years = $years + 1;
$seconds = $seconds - 31536000;
}
while ($seconds >= 86400) {
$days = $days + 1;
$seconds = $seconds - 86400;
}
while ($seconds >= 3600) {
$hours = $hours + 1;
$seconds = $seconds - 3600;
}
while ($seconds >= 60) {
$minutes = $minutes + 1;
$seconds = $seconds - 60;
}
These blocks decrement the value of the $seconds variable to calculate how many years, days, hours and minutes have gone by. Because the number of days in each month differs, it is not practical to count that number.
-
5
Type the following:
echo $years . " years, " . $days . " days, " . $hours . " hours, ".
$minutes . " minutes, " . $seconds . " seconds.";
}
?>
These lines simply display the time difference to the website visitor using the echo statement and then close the timeDiff function and PHP tag.
-
6
Type the following:
echo timeDiff("2011-06-13 12:15:07","2011-07-09 18:35:15");
echo timeDiff("2010-04-16 12:00:00","2011-04-16 12:00:00");
echo timeDiff("2012-12-01 00:00:00",date('Y-m-d h:i:s', time()));
echo timeDiff(date('Y-m-d h:i:s', time()),"2012-12-01 00:00:00");
These four lines are four examples of the timeDiff function at work. The first calculates the difference and returns zero years, 26 days, six hours, 20 minutes and eight seconds. The second returns exactly one year. The third and fourth both use the PHP date and time functions to create one of the parameters; the exact time on the visitor's computer. Note that they pass the same values but in the opposite order. Because you checked for this at the beginning of the timeDiff function, both return the same results; however, these results change dynamically based on the time and day that a visitor loads the page.
-
1