PHP Function Reference

PHP DateTime - diff() Method



The PHP DateTime::diff() method returns the difference between two DateTime objects. The date_diff() function is an alias of this 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 DateTime::diff() method.

<?php
  //creating a DateTime object
  $date1 = new DateTime("25-Mar-2015");
  $date2 = new DateTime("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 method is used to calculate difference between two DateTime objects.

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

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

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

  //getting the difference between two dates 
  $interval = $date3->diff($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 method can be used to calculate difference between two DateTimeImmutable objects. Consider the example below:

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

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

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

  //getting the difference between two dates 
  $interval = $date3->diff($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