date_diff
Calculates the difference between two dates.
Syntax
date_diff(DateTimeInterface $baseObject, DateTimeInterface $targetObject, bool $absolute = false): DateIntervalParameters
| Parameter | Type | Description |
|---|---|---|
| $baseObject | DateTimeInterface | The first date. |
| $targetObject | DateTimeInterface | The second date. |
| $absolute | bool | Optional. Always return a positive difference. |
Return value
DateIntervalThe interval between the dates.
Examples
Age from a birth date:
<?php
$birth = date_create('1995-08-20');
$today = date_create('2026-03-15');
echo date_diff($birth, $today)->y;Output:
30Notes
The $birth->diff($today) method does the same.
See also
- DateInterval — A span of time: days, months, hours.
- strtotime — Turns a textual date description into a timestamp.
- DateTimeImmutable — A date and time object that cannot be changed: every operation returns a new object.
Related recipes
In the tutorial
- Date and time — Working with dates in PHP — the date function, the DateTimeImmutable class, formats, adding to dates and the difference between them.