DateTimeImmutable deep dive

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.

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

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.

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

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.

$a = new DateTimeImmutable("2026-10-01"); $b = new DateTimeImmutable("2026-10-06"); $d = $a->diff($b); echo $d->days; echo $d->format("%d days");

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.

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

Next: Recurring dates

Article author: Arthur Isaev