How can the DateTimeImmutable class in PHP be utilized to handle time calculations more efficiently?

The DateTimeImmutable class in PHP can be utilized to handle time calculations more efficiently by ensuring that the original object remains unchanged when operations are performed on it. This prevents unexpected side effects and makes the code more predictable and easier to debug.

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

// Perform time calculations without modifying the original object
$newDatetime = $datetime->modify('+1 day');

// Output the original and modified dates
echo $datetime->format('Y-m-d') . "\n"; // Output: 2022-01-01
echo $newDatetime->format('Y-m-d') . "\n"; // Output: 2022-01-02