English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP date_create_immutable() Function

PHP Date & Time Function Manual

The date_create_immutable() function returns a new DateTimeImmutable object

Definition and Usage

date_create_immutable() is an alias of DateTimeImmutable::__construct(). This function accepts a date/Time string and timezone as parameters (optional), and create DateTimeImmutable object.
Unlike DateTime objects, this object does not allow any modifications. It will create a new object and return it if any changes occur. By default, this function creates the current date./Time Object

Syntax

date_create_immutable([$date_time, $timezone]);

Parameter

Serial NumberParameters and Description
1

date_time (optional)

This is the date for which you need to create an immutable DateTime object./Time string (using a supported format).

2

timezone (optional)

This indicates the timezone of the given time.

Return value

The PHP date_create_immutable() function returns the created DateTimeImmutable object.

PHP version

This function was initially introduced in PHP version5.5introduced in version 7.2.0 and can be used in all higher versions.

Online example

The following example demonstratesdate_create_immutable()Usage of the function-

<?php
   $date_string = "2019-08-15 9:25:45";
   $immutable = date_create_immutable($date_string);   
   print_r($immutable);
?>
Test and see‹/›

Output result

DateTimeImmutable Object
(
    [date] => 2019-08-15 09:25:45.000000
    [timezone_type] => 3
    [timezone] => UTC
)

Online example

The following example demonstratesdate_create_immutable()Example-

<?php
   $date_string = "2019-08-15 9:25:45";
   $tz = new DateTimeZone('Indian/Mahe');
   $immutable = date_create_immutable($date_string, $tz);   
   print_r($immutable);   
   print(date_format($immutable,'Y-m-d H:i:s')); 
?>
Test and see‹/›

Output result

DateTimeImmutable Object
(
    [date] => 2019-08-15 09:25:45.000000
    [timezone_type] => 3
    [timezone] => Indian/Mahe
)
2019-08-15 09:25:45

Online example

In the following example, we create an immutable date and add an interval to the normal date object, and display the result value. Since the immutable DateTime object creates and returns a new object when changes occur, you can observe that it changes before and after the change-

<?php
   print("Immutable date: "."\n");
   $date1 = date_create_immutable('1986-09-11');
   $date2 = $date1->add(new DateInterval('P15DP12MP9YT24H'));
   print("Original object value: ".$date1->format('Y-m-d')."\n");
   print("After changed: ".$date2->format('Y-m-d')."\n");
   print("Normal date: "."\n");
   $date3 = date_create('1986-09-11');
   $date4 = $date3->add(new DateInterval('P15DP12MP9YT24H'));
   print("Original object value: ".$date3->format('Y-m-d')."\n");
   print("After changed: ".$date4->format('Y-m-d')."\n");
?>
Test and see‹/›

Output result

Immutable date: 
Original object value: 1986-09-11
After changed: 1996-09-27
Normal date: 
Original object value: 1996-09-27
After changed: 1996-09-27