How can PHP be used to format date differences in terms of months, weeks, and days, rather than just total days?

To format date differences in terms of months, weeks, and days in PHP, you can use the DateTime and DateInterval classes to calculate the difference between two dates and then extract the individual components (months, weeks, days) from the interval. You can then format and display these components as needed.

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

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

$months = $interval->m;
$weeks = floor($interval->d / 7);
$days = $interval->d % 7;

echo "Difference: $months months, $weeks weeks, $days days";