PHP Function Reference

PHP DateTimeImmutable - createFromInterface() Method



The PHP DateTimeImmutable::createFromInterface() method returns a new DateTimeImmutable object encapsulating the given DateTimeInterface object.

Syntax

public DateTimeImmutable::createFromInterface(object)

Parameters

object Required. Specify the DateTimeInterface object that needs to be converted to a 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 a immutable date time objects

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

<?php
$date1 = new DateTime("10-Mar-2015 5:10:25 Europe/London");
$date2 = new DateTimeImmutable("25-Oct-2015 5:10:25 Europe/London");

//creating immutable date time objects using interface
$mutable1 = DateTimeImmutable::createFromInterface($date1);
$mutable2 = DateTimeImmutable::createFromInterface($date2);

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

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

❮ PHP Date and Time Reference