What are some best practices for calculating time differences in PHP and displaying them in a user-friendly format?

When calculating time differences in PHP, it's important to use the DateTime class for accurate calculations. To display the time difference in a user-friendly format, you can use the DateInterval class to format the result in terms of hours, minutes, and seconds.

// Calculate time difference
$datetime1 = new DateTime('2022-01-01 08:00:00');
$datetime2 = new DateTime('2022-01-01 09:30:00');
$interval = $datetime1->diff($datetime2);

// Display time difference in a user-friendly format
echo $interval->format('%h hours, %i minutes, %s seconds');