How can you calculate the difference between two dates in PHP?

To calculate the difference between two dates in PHP, you can use the DateTime class to create DateTime objects for the two dates and then calculate the difference using the diff() method. This method returns a DateInterval object which can be used to extract the difference in days, months, years, etc.

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

$interval = $date1->diff($date2);

echo "Difference: " . $interval->format('%R%a days');