PHP date_diff() Function

Example

Calculate the difference between two dates:

<?php
$date1=date_create("1984-01-28");
$date2=date_create("1980-10-15");
$diff=date_diff($date1,$date2);
?>

Run Example

Definition and Usage

The date_diff() function returns the difference between two DateTime objects.

Syntax

date_diff(datetime1,datetime2,absolute);
Parameter Description
datetime1 Required. Specifies a DateTime object.
datetime2 Required. Specifies a DateTime object.
absolute Optional. Specifies a boolean value. TRUE indicates that the interval/difference must be positive. The default is FALSE.

Technical Details

Return Value: Returns a DateInterval object representing the difference between two dates if successful. Returns FALSE if failed.
PHP Version: 5.3+