PHP Function Reference

PHP DateTime - createFromImmutable() Method



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

Syntax

public DateTime::createFromImmutable(object)

Parameters

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

Return Value

Returns a new DateTime instance.

Example: creating a mutable date time object

The example below shows the usage of DateTime::createFromImmutable() method.

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

//creating a mutable date time object 
//from immutable date time object
$mutable = DateTime::createFromImmutable($date);

//displaying the result
echo $date->format('d-M-Y H:i:s P')."\n";
echo $mutable->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