PHP | date_diff() Function
November 24, 2020
Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the date_diff() Function in PHP.
The date_diff() function in PHP is an alias of the DateTime::diff(). It accepts two DateTime objects as parameters and then returns the difference between them.
The date_diff() function in PHP is an alias of the DateTime::diff(). It accepts two DateTime objects as parameters and then returns the difference between them.
Syntax
Following below is the syntax to use this function -
date_diff($datetime1, $datetime2[, $absolute])
READ: PHP | time() Function
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | datetime1(Mandatory) This is a DateTime object, representing one of the dates for the comparison. |
2 | $datetime2 (Mandatory) This is a DateTime object, representing one of the dates for the comparison. |
3 | $absolute (Optional) A boolean value representing whether interval difference should be Must be positive |
Return Value
This built-in function returns the DateInterval object specifying the difference between the two given dates. In case of failure, it returns the boolean value false.
PHP Version
This function was first introduced as part of core PHP v 5.3.0 and, it works with all the later versions.
Example1
Following example demonstrates the usage of the PHP date_diff() function -
<?php //Creating a DateTime object $date1 = date_create("25-09-1989"); $date2 = date_create("1-09-2012"); $interval = date_diff($date1, $date2); print($interval->format('%Y years %d days')); ?>
Output
When the above code is executed, it will produce the following result -
22 years 7 days
Example2
The following example calculates the difference between a given date and a current date -
<?php $date1 = date_create("25-09-1989"); $date2 = date_create(); $interval = date_diff($date1, $date2); print($interval->format('%Y years %d days')); ?>
Output
When the above code is executed, it will produce the following result -
31 years 0 days
Example3
Try the following example -
<?php //Creating a DateTime object $date1 = date_create("25-09-2012"); $date2 = date_create("1-09-2014"); $interval = date_diff($date1, $date2); print($interval->format('%Y years %m months %d days')); print("\n"); $date3 = date_create("25-09-1989"); $date4 = date_create("19-03-2012"); $interval = date_diff($date3, $date4); print($interval->format('%Y years %m months %d days')); print("\n"); $date5 = date_create("16-11-2002"); $date6 = date_create("12-09-2014"); $interval = date_diff($date5, $date6); print($interval->format('%Y years %m months %d days')); print("\n"); $date7 = date_create("25-09-1989"); $date8 = date_create("1-09-2012"); $interval = date_diff($date7, $date8); print($interval->format('%Y years %m months %d days')); ?>
Output
When the above code is executed, it produces the following result -
01 years 11 months 7 days 22 years 5 months 23 days 11 years 9 months 27 days 22 years 11 months 7 days
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP date_parse_from_format() Function.
Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.