In this article, you will learn how to get the difference between two dates in PHP. The date_diff() calculates and returns the difference between two Date/Time objects. A result is also a DateTime object.
This function is very useful and important for both the Database and script logics.
What is the syntax of th date_diff() function in PHP?
date_diff(datetime1, datetime2, absolute)
Parameter | Description |
---|---|
datetime1 | First DateTime Object – Required |
datetime2 | Second DateTime Object – Required |
absolute | When set to true – Difference must be positive By default it is false |
Examples of the date_diff() function
Example 1. In the following example. we calculate the difference between the two dates.
<?php
$date_1=date_create("2021-01-11");
$date_2=date_create("1998-01-04");
$diff=date_diff($date_1,$date_2);
?>