date_diff()
date_diff() — Alias of DateTime::diff().
Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.3.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
date_diff() is an alias of DateTime::diff(): same parameters, same return value.
Signature
date_diff(DateTimeInterface $baseObject, DateTimeInterface $targetObject, bool $absolute = false): DateInterval
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $datetime | mixed | The date to compare to. |
| $absolute | bool | Should the interval be forced to be positive? |
Return value
The DateInterval object represents the difference between the two dates. The absolute parameter only affects the invert property of a DateInterval object. The return value more specifically represents the clock-time interval to apply to the original object ($this or $originObject) to arrive at the $targetObject. This process is not always reversible. The method is aware of DST changeovers, and hence can return an interval of 24 hours and 30 minutes, as per one of the examples. If you want to calculate with absolute time, you need to convert both the $this/$baseObject, and $targetObject to UTC first.
Example
A runnable snippet:
$origin = new DateTimeImmutable('2009-10-11'); $target = new DateTimeImmutable('2009-10-13'); $interval = $origin->diff($target); echo $interval->format('%R%a days');
Output:
+2 days
All PHP 8.5 built-in functions
Reference card built from the PHP manual page date_diff() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.