PHP | DateTime diff() Function Last Updated : 10 Oct, 2019 Comments Improve Suggest changes Like Article Like Report The DateTime::diff() function is an inbuilt function in PHP which is used to return the difference between two given DateTime objects. Syntax: Object oriented style: DateInterval DateTime::diff( DateTimeInterface $datetime2, bool $absolute = FALSE ) or DateInterval DateTimeImmutable::diff( DateTimeInterface $datetime2, bool $absolute = FALSE ) or DateInterval DateTimeInterface::diff( DateTimeInterface $datetime2, bool $absolute = FALSE ) Procedural style: DateInterval date_diff( DateTimeInterface $datetime1, DateTimeInterface $datetime2, bool $absolute = FALSE ) Parameters: This function uses two parameters as mentioned above and described below: $datetime: This parameter holds the date to compare with first date. $absolute: This parameter forced the interval to be positive. Return Value: This function return the difference between two given DateTime objects. Below programs illustrate the DateTime::diff() function in PHP: Program 1: php <?php // Initialising the two datetime objects $datetime1 = new DateTime('2019-9-10'); $datetime2 = new DateTime('2019-9-15'); // Calling the diff() function on above // two DateTime objects $difference = $datetime1->diff($datetime2); // Getting the difference between two // given DateTime objects echo $difference->format('%R%a days'); ?> Output: +5 days Program 2: php <?php // Initialising the two datetime objects $datetime1 = new DateTime('2019-8-10'); $datetime2 = new DateTime('2019-9-10'); // Calling the diff() function on above // two DateTime objects $difference = $datetime1->diff($datetime2); // Getting the difference between two // given DateTime objects echo $difference->format('%R%a days'); ?> Output: +31 days Reference: https://www.php.net/manual/en/datetime.diff.php Comment More infoAdvertise with us Next Article PHP | DateTime diff() Function K Kanchan_Ray Follow Improve Article Tags : Web Technologies PHP PHP-date-time PHP-function Similar Reads PHP | date_diff() Function The date_diff() is an inbuilt function in PHP which is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure. Syntax: date_diff($datetime1, $datetime2); Parameters: The date_diff() function accepts two parameters a 2 min read PHP | DateTime format() Function The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format. Syntax: Object oriented style string DateTime::format( string $format ) or string DateTimeImmutable::format( string $format ) or string DateTimeInterface::f 1 min read PHP | DateTime sub() Function The DateTime::sub() function is an inbuilt function in PHP which is used to subtract a number of days, months, years, hours, minutes and seconds from a created DateTime object. Syntax: Object oriented style: DateTime DateTime::sub( DateInterval interval ) Procedural style: DateTime date_sub( DateTim 2 min read PHP | DateTime add() Function The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object. Syntax: Object oriented style: DateTime DateTime::add( DateInterval $interval ) Procedural style: DateTime date_add( DateT 2 min read PHP | DateTime modify() Function The DateTime::modify() function is an inbuilt function in PHP which is used to modify or can alter the timestamp of a DateTime object. Syntax: Object oriented style: DateTime DateTime::modify( string $modify ) Procedural style: DateTime date_modify( DateTime $object, string $modify ) Parameters: Thi 2 min read Like