English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Function Manual
The date_create_from_format() function parses a date-time string based on the given format.
The date_create_from_format() function is an alias of DateTime::createFromFormat(). It can be used to create a DateTime object.
This function accepts a time string and a format string as parameters, parses the given time string in the specified format, and returns the result as a DateTime object.
date_create_from_format($date)
Serial Number | Parameters and Description |
---|---|
1 | format (required) This is a string value representing the format of the given time 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_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.3introduced in PHP version 5.2.0 and is available in all higher versions.
The following example demonstratesdate_create_from_format()Usage of the function-
<?php //Create a DateTime object $date = "25-Mar-1989"; $format = "d-M-Y");; $res = date_create_from_format($format, $date); print(date_format($res, "Y-m-d")); ?>Test to see‹/›
Output Result
1989-03-25
Pass the value to the optional timezone parameter
<?php //Create a DateTime object $date = "25-Mar-1989"; $format = "d-M-Y");; $tz = new DateTimeZone('Indian/Mahe'); $res = date_create_from_format($format, $date, $tz); print date_format($res, "Y-m-d"); ?>Test to see‹/›
Output Result
1989-03-25
The following example demonstrates the date_create_from_format() function with different formats
<?php $res1 = date_create_from_format("j.n.Y", "25.8.2014"); print(date_format($res1, "Y-m-d")); print("\n"); $res2 = date_create_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 to see‹/›
This will produce the following output-
2014-08-25 2014-08-25 12:20:25