Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

date_diff

Calculates the difference between two dates.

Syntax

date_diff(DateTimeInterface $baseObject, DateTimeInterface $targetObject, bool $absolute = false): DateInterval

Parameters

ParameterTypeDescription
$baseObjectDateTimeInterfaceThe first date.
$targetObjectDateTimeInterfaceThe second date.
$absoluteboolOptional. 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:

30

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.