How To Calculate Difference Between Two Dates in PHP
PHP How ToCalculating the difference between two dates is not as easy as it sounds. There are a lot of things that can go wrong if you are not careful. For example, you have to take both leap years and daylight saving time into consideration. Simply converting two dates to a timestamp and subtracting the values is not reliable in all cases. The PHP DateTime class takes care of all this.
Get the Difference Between Two Dates Using DateTime::diff()
The DateTime class has a diff() method which calculates the difference between two DateTimeobjects. It returns a DateInterval object which contains information like the number of years, months and days between two given dates.
Once you have the DateInterval, you can use the format() method to format it according to your needs. This method accepts different characters as part of its string parameter. All these characters must be prefixed by a percent sign (%).
Here is a list of all the format characters and their usage:
- %: This will output a literal % character.
- Y: This will output years numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 15.
- y: This will output years numerically without any leading zeroes. Examples are 1, 3, 15.
- M: This will output months numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 05, 28.
- m: This will output months numerically without any leading zeroes. Examples are 1, 3, 12.
- D: This will output days numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 09, 29.
- d: This will output days numerically without any leading zeroes. Examples are 1, 9, 31.
- a: This will output the total number of days computed by DateTime::diff() or (unknown) otherwise.
- H: This will output hours numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 23.
- h: This will output hours numerically without any leading zeroes. Examples are 1, 23.
- I: This will output minutes numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 59.
- i: This will output minutes numerically without any leading zeroes. Examples are 1, 3, 59.
- S: This will output seconds numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 57.
- s: This will output seconds numerically without any leading zeroes. Examples are 1, 3, 57.
- F: This will output microseconds numerically with at least 6 digits. Single digits will be followed by leading zero. Examples are 007701, 052738, 428291.
- f: This will output microseconds numerically with at least 6 digits. Single digits will be followed by leading zero. Examples are 7701, 52738, 428291.
- R: This will output “-” when negative and “+” when positive.
- r: This will output “-” when negative and nothing when positive.
Example: Calculated the number of years, months and days between two dates.
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);
// Output: 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);
// Output: 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
The DateTime class takes care of everything for you like different number of days in different months, leap years and daylight saving time.
Get the Number of Days Between Two Dates
Once you have computed the difference between two dates using the diff() method, getting the number of days between them is easy. You just have to pass the %a character sequence to the format() method. The days returned by %D or %d and %a are different.
The %D and %d character sequences return the number of days between two dates after subtracting the number of months and years. Their value will never be greater than 31. On the other hand, the %a character sequence returns the total number of days that have passed between two dates.
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);
// Output: 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');
// Output: Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');