How to Calculate Time Between Two Dates in PHP
PHP is a general-purpose server-side scripting language that was originally designed for creating dynamic Web pages. PHP is an open-source language that can be configured to run on UNIX, Linux, Windows or Mac OSX, supports a wide range of databases and has a comprehensive library of built-in extensions. The PHP core installation contains many date and time functions and formats that can be used and manipulated by those creating dynamic Web pages, writing server scripts or coding stand-alone applications.
Things You'll Need
- Text editor
- Web server with PHP 5 or later installed and configured
- Web browser
Instructions
-
-
1
Open a text editor and create a new file named timeBetweenDates.php. Save the file on a Web server in a location that has access to PHP.
-
2
Edit timeBetweenDates.php. Type a <?PHP PHP open tag and a ?> PHP close tag in the file. When the PHP program parses the file, it will consider text placed between these two tags code.
<?PHP
?>
-
-
3
Use the PHP date_default_timezone_set() function to set the server's time zone to America/New_York. The time zone will be set for the duration of the script.
<?PHP
date_default_timezone_set('America/New_York');
?>
-
4
Declare a variable named $start_date and use the strtotime() PHP date/time function to parse the date/time into a UNIX time stamp relative to now. Use the date July 30, 1970, or 7/30/1970.
<?PHP
date_default_timezone_set('America/New_York');
$start_date = strtotime('7/30/1970');
?>
-
5
Declare a variable named $end_date and use the strtotime() PHP date/time function to parse the date/time into a UNIX time stamp relative to now. Use the date July 30, 2011, or 7/30/2011.
<?PHP
date_default_timezone_set('America/New_York');
$start_date = strtotime('7/30/1970');
$end_date = strtotime('7/30/2011');
?>
-
6
Declare a variable named $years_between_dates to hold the result of the date calculation. Use the abs() function to subtract the $end_date variable from the $start_date variable and divide the result by 86400 (the number of seconds in a day). The $years_between_dates variable now holds the number of days that have passed between $start_date and $end_date.
<?PHP
date_default_timezone_set('America/New_York');
$start_date = strtotime('7/30/1970');
$end_date = strtotime('7/30/2011');
$years_between_dates = abs(($start_date-$end_date)/86400);
?>
-
7
Use the round() function to divide the $years_between_dates variable by 365 (the number of days in a year) rounded to the closest year. Store the result back in the $years_between_dates variable.
<?PHP
date_default_timezone_set('America/New_York');
$start_date = strtotime('7/30/1970');
$end_date = strtotime('7/30/2011');
$years_between_dates = abs(($start_date-$end_date)/86400);
$years_between_dates = round($years_between_dates/365);
?>
-
8
Use the PHP echo() command to write the text "The number of years between the two dates is: " to the Web page. Concatenate the text with the variable $years_between_dates using the PHP concatenation operator (a period). Save and close timeBetweenDates.php.
<?PHP
date_default_timezone_set('America/New_York');
$start_date = strtotime('7/30/1970');
$end_date = strtotime('7/30/2011');
$years_between_dates = abs(($start_date-$end_date)/86400);
$years_between_dates = round($years_between_dates/365);
echo "The number of years between the two dates is: " . $years_between_dates;
?>
-
9
Open a Web browser and request timeBetweenDates.php from the Web server. The difference between the two dates will be displayed in years.
-
1
Tips & Warnings
Dozens of time zone values are available to you when you are setting the default time zone used in date/time functions. See the Resources section for a comprehensive list.
PHP date/time functions are part of PHP core and do not require a separate installation.
PHP date/time functions are dependent on server settings and may be set in php.ini.
The strtotime() function can parse many date/time formats, including relative dates, such as "next Saturday" or "+7 hours."
The date_default_timezone_set function is available in PHP 5 and up.
References
Resources
- Photo Credit Comstock/Comstock/Getty Images