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 // PHP program to illustrate // date_diff() function // creates DateTime objects $datetime1 = date_create('2017-06-28'); $datetime2 = date_create('2018-06-28'); // calculates the difference between DateTime objects $interval = date_diff($datetime1, $datetime2); // printing result in days format echo $interval->format('%R%a days'); ?> |
+365 days
Program 2:
<?php // PHP program to illustrate // date_diff() function // difference only in year $datetime1 = date_create('2017-06-28'); $datetime2 = date_create('2018-06-28'); $interval = date_diff($datetime1, $datetime2); echo $interval->format('%R%a days') . "\n"; // Difference only in months $datetime1 = date_create('2018-04-28'); $datetime2 = date_create('2018-06-28'); $interval = date_diff($datetime1, $datetime2); echo $interval->format('%R%a days') . "\n"; // Difference in year, month, days $datetime1 = date_create('2017-06-28'); $datetime2 = date_create('2018-04-05'); $interval = date_diff($datetime1, $datetime2); echo $interval->format('%R%a days') . "\n"; ?> |
+365 days +61 days +281 days
Reference:http://php.net/manual/en/function.date-diff.php
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AndroGarcia

