The date_diff Function

The date_diff function finds the difference between two dates. The dates must be objects created by the function date_create. The function returns a 'date' object, which can be formatted using the format method.

Syntax

phpdate_diff(DateTimeInterface $base, DateTimeInterface $target, bool $absolute = false): DateInterval

Example

Let's find the difference in days between 2025-12-31 and 2026-01-05:

php<?php
  $date1 = date_create('2025-12-31');
  $date2 = date_create('2026-01-05');
  
  $diff = date_diff($date1, $date2);
  echo $diff->format('%a days');
?>

Code execution result:

php5 days

See Also

  • the date_modify function,
    which adds or subtracts time intervals from a date