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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_create_immutable_from_format() Function Usage and Examples

PHP Date & Time Functions Manual

The date_create_immutable_from_format() function parses a time string according to the specified format

Definition and Usage

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

Syntax

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

Parameter

Serial NumberParameters and Description
1

format (required)

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

2

time (required)

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

3

timezone (optional)

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

Return Value

The date_create_immutable_from_format() function returns a DateTime object representing the parsed time. If it fails, this function returns a boolean valuefalse.

PHP Version

This function was originally introduced in PHP version5.5introduced in version 7.1.0 and is available in all higher versions.

Online Example

The following examples demonstratedate_create_immutable_from_format()Function Usage-

<?php
   //Create a DateTime object
   $date = "1989-08-25";
   $format = "Y-m-d");
   $res = date_create_immutable_from_format($format, $date);
   print("Date: ".date_format($res, "Y-m-d"));
?>
Test and see‹/›

Output Result

Date: 1989-08-25

Online Example

Now, let's try passing the optional timezone parameter-

<?php
   //Create a DateTime object
   $date = "1989-08-25";
   $format = "Y-m-d");
   $tz = new DateTimeZone('Asia/Shanghai');  
   $res = date_create_immutable_from_format($format, $date, $tz);
   print date_format($res, "Y-m-d");
?>
Test and see‹/›

Output Result

1989-08-25

Online Example

The following example demonstrates the usage of date_create_immutable_from_format() 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"));  
?>
Test and see‹/›

This will produce the following output-

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