How can the use of DateTimeImmutable improve the accuracy and efficiency of date calculations in PHP?

Using DateTimeImmutable in PHP can improve the accuracy and efficiency of date calculations by ensuring that any modifications to a DateTime object result in a new object being created, rather than modifying the original object. This prevents unexpected changes to date values and makes code easier to reason about.

// Create a DateTimeImmutable object
$date = new DateTimeImmutable('2022-01-01');

// Add 1 day to the date without modifying the original object
$newDate = $date->modify('+1 day');

// Output the original and modified dates
echo $date->format('Y-m-d') . "\n";
echo $newDate->format('Y-m-d');