PHP Tutorial - PHP date_diff() Function






Definition

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

Syntax

PHP date_diff() Function has the following syntax.

date_diff(datetime1,datetime2,absolute);

Parameter

ParameterIs RequiredDescription
datetime1Required.DateTime object
datetime2Required.DateTime object
absoluteOptional.TRUE indicates that the interval/difference MUST be positive. Default is FALSE

Return

PHP date_diff() Function returns a DateInterval object on success that represents the difference between the two dates.

PHP date_diff() Function returns FALSE on failure.





Example

Calculate the difference between two dates


<?php
$date1=date_create("2011-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
?>