What exactly is the Datetime::createFromImmutable() method for?

Asked

Viewed 93 times

3

What exactly is the method for DateTime::createFromImmutable()? Implemented according to the release note of version 7.3.0 of PHP.

1 answer

3


Attending to the request, the method will serve for you to create an object DateTime from an object DateTimeImmutable.

$immutable = new DateTimeImmutable(); // objeto imutável
$mutable = DateTime::createFromImmutable($immutable); // objeto mutável

This method had already been created before and was removed later. It supports what exists in reverse. That is, create a DateTimeImmutable from a DateTime in this way:

$mutable = new DateTime(); // objeto mutável
$immutable = DateTimeImmutable::createFromMutable($mutable); // objeto imutável

Take an example how it could be done before this method:

$dateTime = new \DateTime();
$dateTime->setTimestamp($dateTimeImmutable->getTimestamp());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.