PHP Function Reference

PHP DateTimeImmutable - createFromMutable() Method



The PHP DateTimeImmutable::createFromMutable() method returns a new DateTimeImmutable object encapsulating the given DateTime object.

Syntax

public DateTimeImmutable::createFromMutable(object)

Parameters

object Required. Specify the mutable DateTime object that needs to be converted to an immutable version. This object is not modified, but instead a new DateTimeImmutable object is created containing the same date time and timezone information.

Return Value

Returns a new DateTimeImmutable instance.

Example: creating an immutable date time object

The example below shows the usage of DateTimeImmutable::createFromMutable() method.

<?php
//creating a DateTime object
$date = new DateTime("10-Mar-2015 5:10:25 Europe/London");

//creating a immutable date time object 
//from mutable date time object
$immutable = DateTimeImmutable::createFromMutable($date);

//displaying the result
echo $date->format('d-M-Y H:i:s P')."\n";
echo $immutable->format('d-M-Y H:i:s P')."\n";
?>

The output of the above code will be:

10-Mar-2015 05:10:25 +00:00
10-Mar-2015 05:10:25 +00:00

❮ PHP Date and Time Reference