What are the best practices for handling date and time functions in PHP to ensure accurate results, especially when dealing with shifts that span across midnight?

When dealing with shifts that span across midnight in PHP, it is important to handle date and time functions carefully to ensure accurate results. One approach is to use the DateTime class along with the modify() method to adjust the dates and times accordingly. Additionally, consider using the date_diff() function to calculate the duration of the shift accurately.

// Example code snippet to handle shifts that span across midnight
$shiftStart = new DateTime('2022-01-01 22:00:00');
$shiftEnd = new DateTime('2022-01-02 06:00:00');

// Calculate the duration of the shift
$duration = date_diff($shiftStart, $shiftEnd);

echo "Shift duration: " . $duration->format('%H hours %i minutes');