What is the correct way to calculate the difference between two dates in PHP using DateTime objects?

When calculating the difference between two dates in PHP using DateTime objects, you can create two DateTime objects representing the dates you want to compare. Then, you can use the `diff()` method to calculate the difference between the two dates. This method returns a DateInterval object which you can use to get the difference in days, months, years, etc.

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

$interval = $date1->diff($date2);
echo $interval->format('%R%a days');