In what scenarios would it be recommended to use the DateTimeImmutable class over the DateTime class in PHP for handling dates and times?

The DateTimeImmutable class in PHP should be used when you want to ensure that a date/time object remains immutable, meaning that any modifications to the object will not affect the original object. This is useful in scenarios where you need to work with dates and times without altering the original values. It is recommended to use DateTimeImmutable when you need to handle date/time calculations or comparisons without changing the original date/time object.

// Example of using DateTimeImmutable to handle dates and times
$originalDate = new DateTimeImmutable('2022-01-01');
$newDate = $originalDate->modify('+1 day');

echo $originalDate->format('Y-m-d'); // Output: 2022-01-01
echo $newDate->format('Y-m-d'); // Output: 2022-01-02