PHP Function Reference

PHP date_diff() Function



The PHP date_diff() function returns the difference between two DateTime objects. This function is an alias of DateTime::diff() method.

Note: This method is defined under DateTimeInterface interface. DateTime and DateTimeImmutable classes inherit this method from DateTimeInterface interface.

Syntax

//Object-oriented style
public DateTime::diff(targetObject, absolute)
public DateTimeImmutable::diff(targetObject, absolute)
public DateTimeInterface::diff(targetObject, absolute)

//Procedural style
date_diff(baseObject, targetObject, absolute)

Parameters

baseObject Required. For procedural style only: A DateTime object for comparison.
targetObject Required. Another DateTime object for comparison.
absolute Optional. A boolean value. If set to true, interval is forced to be positive. Default is false.

Return Value

Returns the DateInterval object represents the difference between the two dates or false on failure.

Example: using both styles

The example below shows the usage of date_diff() function.

<?php
  //creating a DateTime object
  $date1 = date_create("25-Mar-2015");
  $date2 = date_create("10-Oct-2016");

  //getting the difference between two dates 
  //using Object-oriented style
  $interval = $date1->diff($date2);
  echo "Difference is: "
       .$interval->format("%y years %m months %d days")."\n";

  //getting the difference between two dates 
  //using Procedural style
  $interval = date_diff($date1, $date2);
  echo "Difference is: "
       .$interval->format("%y years %m months %d days")."\n";
?>

The output of the above code will be:

Difference is: 1 years 6 months 15 days
Difference is: 1 years 6 months 15 days

Example: Difference of DateTime objects

Consider the example below where this function is used to calculate difference between two DateTime objects.

<?php
  //creating a DateTime object
  $date1 = date_create("10-Oct-2013");
  $date2 = date_create("25-Mar-2016");

  //getting the difference between two dates 
  $interval = date_diff($date1, $date2);
  echo "Difference is: "
       .$interval->format("%y years %m months %d days")."\n";

  //creating a DateTime object
  $date3 = date_create("25-12-2005");
  $date4 = date_create("18-5-2020");

  //getting the difference between two dates 
  $interval = date_diff($date3, $date4);
  echo "Difference is: "
       .$interval->format("%Y years %m months %d days")."\n";
?>

The output of the above code will be:

Difference is: 2 years 5 months 15 days
Difference is: 14 years 4 months 23 days

Example: Difference of DateTimeImmutable objects

Similarly, this function can be used to calculate difference between two DateTimeImmutable objects. Consider the example below:

<?php
  //creating a DateTimeImmutable object
  $date1 = date_create_immutable("19-Nov-2003");
  $date2 = date_create_immutable("25-Jun-2016");

  //getting the difference between two dates 
  $interval = date_diff($date1, $date2);
  echo "Difference is: "
       .$interval->format("%y years %m months %d days")."\n";

  //creating a DateTimeImmutable object
  $date3 = date_create_immutable("25-12-2002");
  $date4 = date_create_immutable("28-8-2020");

  //getting the difference between two dates 
  $interval = date_diff($date3, $date4);
  echo "Difference is: "
       .$interval->format("%Y years %m months %d days")."\n";
?>

The output of the above code will be:

Difference is: 12 years 7 months 6 days
Difference is: 17 years 8 months 3 days

❮ PHP Date and Time Reference