PHP | date_create_immutable_from_format() Function

PHP date_create_immutable_from_format() 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_from_format() Function.

The date_create_immutable_from_format() function in PHP is an alias of the PHP DateTimeImmutable::createFromFormat(). It accepts a time and format string as parameters, parses the given time string in the specified format and returns a DateTimeImmutable object.

Syntax

Following below is the syntax to use this function -

date_create_immutable_from_format($date, $time [,$timezone])


Parameter Details

Sr.NoParameter & Description
1

format(Mandatory)

This is a string value representing the format in which you need to parse the given time string.

2

time(Mandatory)

This is a string value representing the time you need to parse.

3

timezone(Optional)

This is an object of DateTimeZone class representing the desired timezone.


Return Value

This built-in function returns a DateTime object representing the parsed time. In case of failure, it returns the boolean value false.

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 utilization of the built-in PHP date_create_immutable_from_format() function -

<?php
   //Creating a DateTime object
   $date = "25-Mar-1989";
   $format = "d-M-Y";
   $res = date_create_immutable_from_format($format, $date);
   print("Date: ".date_format($res, "Y-m-d"));
?>

Output

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

Date: 1989-03-25

Example2

Now, let us try to pass the optional timezone parameter -

<?php
   //Creating a DateTime object
   $date = "25-Mar-1989";
   $format = "d-M-Y";
   $tz = new DateTimeZone('Indian/Mahe');  
   $res = date_create_immutable_from_format($format, $date, $tz);
   print date_format($res, "Y-m-d");
?>

Output

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

1989-03-25

Example3

Following example demonstrates the utilization of the built-in PHP date_create_immutable_from_format() function with different formats -

<?php
   $res1 = date_create_immutable_from_format("j.n.Y", "25.8.2014");
   print(date_format($res1, "Y-m-d"));
   print("\n");
   $res2 = date_create_immutable_from_format('Y-d-m H:i:s', '2014-25-08 12:20:25');
   print(date_format($res2, "Y-m-d H:i:s"));  
?>

Output

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

2014-08-25
2014-08-25 12:20:25


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 PHP date_create_immutable() Function.

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.