Home >>PHP Date Time Functions >PHP date_diff() Function

PHP date_diff() Function

PHP date_diff() Function

PHP date_diff() function is used to calculate the difference between two given DateTime objects. It returns a date time object as output on the success otherwise it returns failure.

Syntax:

date_diff($datetime1, $datetime2, $absolute)

Parameter Values

Parameter Description
datetime1 This is a required parameter. This parameter defines a DateTime object.
datetime2 This is a required parameter. This parameter defines another DateTime object.
absolute This is an optional parameter. This parameter defines a Boolean value. TRUE value indicates that the interval must be positive.

Here is an example of date_diff() function in PHP:

<html>
<body>

<?php

$date1=date_create("2013-03-18");
$date2=date_create("2015-12-23");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");

?>

</body>
</html>
Output:
+1010 days

Here is an example of date_diff() function in PHP:

<html>
<body>
<?php
$date1=date_create("2013-03-18");
$date2=date_create("2015-12-23");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
</body>
</html>
Output:
+1010 days

Example 2

<html>
<body>
<?php
function FinddateDiff($startDate, $endDate) 
{  
$start_d = strtotime($startDate);  
$end_d = strtotime($endDate);  
  
$diff = $end_d - $start_d;  
return round($diff / 86400);  
}  
echo FinddateDiff("2020-01-21", "2020-01-31");  
?>
</body>
</html>
Output:10

No Sidebar ads