How can DateTime objects and date_diff function be used to improve the accuracy of time progress calculations in PHP?

When calculating time progress in PHP, using DateTime objects and the date_diff function can improve accuracy by allowing for precise calculations of the time difference between two dates or times. This can be particularly useful when dealing with time intervals that cross over into different days, months, or years.

// Create DateTime objects for the start and end times
$start = new DateTime('2022-01-01 08:00:00');
$end = new DateTime('2022-01-02 10:30:00');

// Calculate the time difference between the two DateTime objects
$interval = $start->diff($end);

// Output the total number of hours and minutes between the two times
echo "Time elapsed: " . $interval->h . " hours and " . $interval->i . " minutes";