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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_create_from_format() Function Usage and Examples

PHP Date & Time Function Manual

The date_create_from_format() function parses a date-time string based on the given format.

Definition and Usage

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.

Syntax

date_create_from_format($date)

Parameter

Serial NumberParameters 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.

Return Value

The date_create_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.3introduced in PHP version 5.2.0 and is available in all higher versions.

Online Example

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

Online Example

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

Online Example

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