We now have a youtube channel. Subscribe!

PHP | date_create_immutable() Function

PHP date_create_immutable() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_create_immutable() Function.

The built-in PHP date_create_immutable() function is an alias of the built-in PHP DateTimeImmutable::_construct(). This function accepts a DateTime string and time zone string as parameters (optional) and creates a DateTime object.

Unlike DateTime object, this object does not allow any modifications, it creates a new object in case of changes and returns it. By default this built-in function creates of the current date and time.

Syntax

Following below is the syntax to use this function -

date_create_immutable([$date_time, $timezone]);


Parameter Details

Sr.NoParameter & Description
1

date_time (Optional)

This is the date/time string (in supported formats) for which you need to create an immutable DateTime object.

2

timezone (Optional)

This represents the timezone of the given time.


Return Value

This built-in function returns the created DateTimeImmutable object.

PHP Version

This function was first introduced as part of core PHP version 5.5.0 and, it works with all of the later versions.

Example1

Following example demonstrates the use of date_create_immutable() function -

<?php
   $date_string = "2019-08-15 9:25:45";
   $immutable = date_create_immutable($date_string);   
   print_r($immutable);
?>

Output

When the above code is executed, it will produce the following result -

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

Example2

The following is an example of the date_create_immutable() function in PHP with time zone parameter -

<?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')); 
?>

Output

When the above code is executed, it will produce the following result -

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

Example3

In the following example below, we have created an immutable and a normal date object, intervals added to them and showed the resultant values. Since immutable DateTime object creates and returns a new object in case of a trial to change, you can observe that it vary before and after 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 Change: ".$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 Change: ".$date4->format('Y-m-d')."\n");
?>

Output

When the above code is executed, it will produce the following result -

Immutable Date:
Original Object Value: 1986-09-11
After Change: 1996-09-27
Normal Date:
Original Object Value: 1996-09-27
After Change: 1996-09-27


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the timezone_version_get() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain