How can PHP be used to calculate the difference between two dates in years and days when both dates are in timestamp format?

To calculate the difference between two dates in years and days when both dates are in timestamp format, we can convert the timestamps to DateTime objects and then use the diff() method to calculate the difference in days. We can then divide the total days by 365 to get the difference in years and use the remainder to get the remaining days.

$date1 = new DateTime('@timestamp1');
$date2 = new DateTime('@timestamp2');

$interval = $date1->diff($date2);
$years = floor($interval->days / 365);
$days = $interval->days % 365;

echo "Difference: $years years, $days days";