DateTimeImmutable behaves like DateTime for formatting and calculations, but methods such as modify(), add() and setDate() return a new object instead of changing the original.
$original = new DateTimeImmutable('2026-09-14');
$next = $original->modify('+1 week');
echo $original->format('Y-m-d') . "
";
echo $next->format('Y-m-d');Store the returned object from mutating-style methods; ignoring it leaves the original immutable value unchanged.
$date1=new DateTime('2018-04-23'); // date object created.
$date1->add(new DateInterval('P1Y3M')); // interval of 1 year 3 months added
echo $date1->format('Y-M-d'); // Output is 2019-Jul-23
/// by using DateTimeImmutable ///
$date1=new DateTimeImmutable('2018-04-23'); // date object created .
$date1->add(new DateInterval('P1Y3M')); // interval of 1 year 3 months added
echo $date1->format('Y-M-d'); // Output is 2018-Apr-23
In case of date object created using DateTimeImmutable after adding the interval the original value never changes.
$date = new DateTimeImmutable('now', new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
$date = new DateTimeImmutable('2023-12-25');
$newDate = $date->modify('+1 day');
echo $date->format('Y-m-d'); // Output: 2023-12-25
echo $newDate->format('Y-m-d'); // Output: 2023-12-26
$date = new DateTimeImmutable('now');
$newDate = $date->modify('+1 day');
echo $date->format('Y-m-d : H:i:s').'<br>';
echo $newDate->format('Y-m-d : H:i:s');
Output
2026-09-30 : 02:47:46
2026-10-01 : 02:47:46
Using setDate()
$today = new DateTimeImmutable();
echo $today->format('Y-m-d ').'<br>';
$changed = $today->setDate(2025, 11, 13);
echo $changed->format('Y-m-d');
Output
2026-09-30
2025-11-13
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.