What is the purpose of using date_diff in PHP?

The purpose of using date_diff in PHP is to calculate the difference between two dates. This function returns a DateInterval object representing the difference between two dates, which can then be used to extract the interval in years, months, days, hours, minutes, and seconds.

$date1 = new DateTime('2022-01-01');
$date2 = new DateTime('2022-12-31');

$interval = date_diff($date1, $date2);

echo $interval->format('%y years, %m months, %d days');