PHP date_diff() function

PHP date_default_timezone_set() function
PHP date_format() function

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)
ParameterDescription
datetime1First DateTime Object – Required
datetime2Second DateTime Object – Required
absoluteWhen set to true – Difference must be positive
By default it is false
PHP date_diff() method

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);
?>
PHP date_default_timezone_set() function
PHP date_format() function
en English
X
Scroll to Top