You work out a due date from a start date. The due date is right. Then the start date on the invoice is wrong as well, and it's showing the due date.
Nothing crashed and there's no warning. The cause is one line where DateTime did exactly what it was built to do.
DateTime changes itself
DateTime::modify() changes the object you call it on and returns that same object. From the DateTime::modify manual: it alters the timestamp of the object.
```php <?php $start = new DateTime('2026-11-09'); $end = $start->modify('+7 days');
echo $start->format('Y-m-d'), "\n"; // 2026-11-16 echo $end->format('Y-m-d'), "\n"; // 2026-11-16 var_dump($start === $end); // bool(true) ```
$start and $end are the same object, so the start date has moved. The same applies to add(), sub(), setDate() and setTime() on a DateTime.
DateTimeImmutable returns a new object
DateTimeImmutable has the same methods, but each one returns a new object and leaves the original alone.
```php <?php $start = new DateTimeImmutable('2026-11-09'); $end = $start->modify('+7 days');
echo $start->format('Y-m-d'), "\n"; // 2026-11-09 echo $end->format('Y-m-d'), "\n"; // 2026-11-16 ```
There's one new trap to watch for. Because nothing changes in place, a call whose result you don't keep does nothing:
```php $date->modify('+1 day'); // result thrown away, $date unchanged $date = $date->modify('+1 day'); // this is what you meant ```
A small, safe pattern for business dates
Type your functions against DateTimeImmutable (or DateTimeInterface if you accept both). Be explicit about the time zone, and return a new value.
```php <?php function dueDate(DateTimeImmutable $issued, int $days = 30): DateTimeImmutable { return $issued->add(new DateInterval('P' . $days . 'D')); }
$issued = new DateTimeImmutable('2026-11-08', new DateTimeZone('Europe/London')); $due = dueDate($issued);
echo $issued->format('d M Y'), ' -> ', $due->format('d M Y'), "\n"; // 08 Nov 2026 -> 08 Dec 2026 ```
The caller's $issued can't be changed by dueDate(), so you don't have to read the function to trust it.
Switching an existing codebase over
- New code: default to
DateTimeImmutable. - Boundaries: if a library hands you a
DateTime, convert it once with DateTimeImmutable::createFromMutable() (orcreateFromInterface()on PHP 8.0+). Then work with the immutable copy. - Type hints: accept
DateTimeInterfacewhen either class is fine. Ask forDateTimeImmutablewhen you need the guarantee. - Search for reassignment: when you swap
new DateTimefornew DateTimeImmutable, look for->modify(,->add(,->sub(and->set...(calls whose result isn't assigned. Those are the lines that will now silently do nothing.
Why this makes money sense
Wrong dates on invoices, bookings or reminders are support calls you don't get paid for. Making dates immutable removes a whole class of "who changed this?" bugs at very little cost.
Deeper PHP habits live in PHP Done The Correct Way (paperback on Amazon UK, Kindle UK, free companion code on the PHP AIR code page). More practical notes are on the PHP AIR blog, or start at phpair.co.uk.