PHP | date_diff() Function
Last Updated :
12 Feb, 2019
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 as mentioned above and described below:
- $datetime1: It is a mandatory parameter which specifies the first DateTime object.
- $datetime2: It is a mandatory parameter which specifies the second DateTime object.
Return Value: It returns the difference between two DateTime objects otherwise, FALSE on failure.
Below programs illustrate the date_diff() function:
Program 1:
<?php
$datetime1 = date_create('2017-06-28');
$datetime2 = date_create('2018-06-28');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
|
Program 2:
<?php
$datetime1 = date_create('2017-06-28');
$datetime2 = date_create('2018-06-28');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days') . "\n";
$datetime1 = date_create('2018-04-28');
$datetime2 = date_create('2018-06-28');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days') . "\n";
$datetime1 = date_create('2017-06-28');
$datetime2 = date_create('2018-04-05');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days') . "\n";
?>
|
Output:
+365 days
+61 days
+281 days
Reference:http://php.net/manual/en/function.date-diff.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...