DateTimeImmutable deep dive

PHP keeps date and time in objects, and the immutable one is the safe default: every change returns a new object, so a value passed around the code cannot be edited behind your back.

Create

Create a DateTimeImmutable with a date string and an explicit zone. The object then holds a fixed moment that never shifts by accident.

Prefer the constructor with a zone argument over the global default so code behaves the same on every server.

<?php $start = new DateTimeImmutable("2026-10-06 12:00:00", new DateTimeZone("UTC")); echo $start->format("Y-m-d H:i:s"), PHP_EOL;

php create.php

2026-10-06 12:00:00

The zone is part of the object, so the same string read on a server set to another default still names this moment.

Modify returns new

Method modify never edits the object it is called on. It builds and returns a fresh object with the new value.

Assign the result to a variable or chain it directly into format. The original stays ready for other math.

<?php $start = new DateTimeImmutable("2026-10-06", new DateTimeZone("UTC")); $later = $start->modify("+3 days"); echo $start->format("Y-m-d"), PHP_EOL; echo $later->format("Y-m-d"), PHP_EOL;

php modify.php

2026-10-06
2026-10-09

The first line is the original, printed after the call that changed it, so the immutability is visible and not just claimed.

Diff dates

Method diff compares two moments and returns an interval with years, months, days and an invert flag. Property days holds the total day count.

Read invert to learn direction: zero means the second date lies ahead, one means it lies behind.

<?php $a = new DateTimeImmutable("2026-10-01"); $b = new DateTimeImmutable("2026-10-06"); $d = $a->diff($b); echo $d->days, PHP_EOL; echo $d->format("%d days"), PHP_EOL; $back = $b->diff($a); echo $back->invert, PHP_EOL;

php diff.php

5
5 days
1

Five days between the two dates, and the backward comparison flips the invert flag to one while the day count stays positive.

Timezones

A date without a zone is a guess. Attach a DateTimeZone at creation and convert with setTimezone for display.

See the timezones guide for the full list of zone names and DST notes.

<?php $utc = new DateTimeImmutable("2026-10-06 12:00:00", new DateTimeZone("UTC")); $msk = $utc->setTimezone(new DateTimeZone("Europe/Moscow")); echo $utc->format("Y-m-d H:i:s"), PHP_EOL; echo $msk->format("Y-m-d H:i:s"), PHP_EOL;

php zone.php

2026-10-06 12:00:00
2026-10-06 15:00:00

The same moment reads noon in UTC and three in the afternoon in Moscow, and the second object was built from the first without touching it.

Exercises

  1. Build a date from a string with a zone and print it in ISO 8601 with format("c").
  2. Add one month to the last day of January and print the result.
  3. Compare two dates and print the number of full months between them.
  4. Convert a UTC moment to two zones and print both.
  5. Show that setTime() on an immutable object leaves the original time alone.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does modify() do on a DateTimeImmutable object?

    Answer

    It returns a new object and leaves the original alone

    The immutable class never changes in place: each change produces a fresh object.

  2. Which property of a DateInterval holds the total number of days?

    Answer

    days

    The lowercase d field is the day part of the interval; days is the whole count.

  3. What does an invert flag of 1 in a DateInterval mean?

    Answer

    The interval is negative, so the second date lies behind

    Invert marks a backward difference; the day count itself stays positive.

  4. Why pass a DateTimeZone to the constructor?

    Answer

    To make the moment independent of the server default

    An explicit zone fixes the meaning of the string, so another server default cannot move the moment.

  5. What does this program print? <?php $start = new DateTimeImmutable("2026-10-06 12:00:00", new DateTimeZone("UTC")); echo $start->format("Y-m-d H:i:s");

    Answer

    2026-10-06 12:00:00

    The format string asks for the date and the time, and the object holds both.

  6. What does this program print? <?php $start = new DateTimeImmutable("2026-10-06", new DateTimeZone("UTC")); $later = $start->modify("+3 days"); echo $start->format("Y-m-d"), " ", $later->format("Y-m-d");

    Answer

    2026-10-06 2026-10-09

    modify() builds a new object, so the original keeps its date and only the copy moves.

  7. What does this program print? <?php $a = new DateTimeImmutable("2026-10-01"); $b = new DateTimeImmutable("2026-10-06"); echo $a->diff($b)->days;

    Answer

    5

    The two dates are five days apart, and days is the total count of days in the interval.

  8. What does this program print? <?php $utc = new DateTimeImmutable("2026-10-06 12:00:00", new DateTimeZone("UTC")); $msk = $utc->setTimezone(new DateTimeZone("Europe/Moscow")); echo $msk->format("H:i");

    Answer

    15:00

    Moscow is three hours ahead of UTC in October, so noon becomes three in the afternoon.

  9. What does invert hold in the interval returned by $a->diff($b) when $b is later than $a?

    Answer

    0

    A forward difference is not inverted, so the flag is zero.

  10. Which call turns a UTC moment into the same moment in another zone?

    Answer

    setTimezone()

    setTimezone() returns the same instant with the clock of the other zone.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev