# `DateTime::createFromImmutable()`

> Section in “New and Extended Functionality › Date and Time” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/date-and-time/datetimecreatefromimmutable/>

The original `DateTime` class that represents date and time information is, and has always been, mutable. This means that `DateTime` objects can be modified through methods such as `DateTime::add()`.

The `DateTimeImmutable` class, which was added later, is, as its name gives away, immutable. Once created, a `DateTimeImmutable` object cannot be modified. This is great as immutable state should be favored over mutable state.

Ever since its introduction, the `DateTimeImmutable` provides the `createFromMutable()` method to create a `DateTimeImmutable` object based on an existing `DateTime` object:

``` php
$mutable   = new DateTime;
$immutable = DateTimeImmutable::createFromMutable($mutable);
```

The other way around, though, creating a `DateTime` object based on an existing `DateTimeImmutable` object was not as easy. This has been addressed with the introduction of the `DateTime::createFromImmutable()` method:

``` php
$immutable = new DateTimeImmutable;
$mutable   = DateTime::createFromImmutable($immutable);
```

---

Previous: [Date and Time](https://php7explained.com/new-and-extended-functionality/date-and-time/index.html.md) | Next: [High-Resolution Monotonic Timer](https://php7explained.com/new-and-extended-functionality/date-and-time/high-resolution-monotonic-timer/index.html.md)
