English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Functions Manual
The date_create_immutable_from_format() function parses a time string according to the specified format
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.
date_create_immutable_from_format($date, $time [,$timezone])
Serial Number | Parameters 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. |
The date_create_immutable_from_format() function returns a DateTime object representing the parsed time. If it fails, this function returns a boolean valuefalse.
This function was originally introduced in PHP version5.5introduced in version 7.1.0 and is available in all higher versions.
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
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
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