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_from_format() Function.
The date_create_from_format() function is an alias of DateTime::createFromFormat(). By making use of this built-in PHP function you can create a DateTime object.
This function accepts a time string and format string as parameters, parses the given time string in the specified format and returns the result as a DateTime object.
The date_create_from_format() function is an alias of DateTime::createFromFormat(). By making use of this built-in PHP function you can create a DateTime object.
This function accepts a time string and format string as parameters, parses the given time string in the specified format and returns the result as a DateTime object.
Syntax
Following below is the syntax to use this function -
date_create_from_format($date)
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | format(Mandatory) This is a string value representing the format in which you need to parse the given time. |
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 function returns a DateTime object representing the parsed time. In case of failure this function returns the boolean value false.
PHP Version
This function was first introduced as part of the core PHP v 5.3.0 and, it works with all of the later versions.
Example1
Below example shows the usage of the date_create_from_format() function -
<?php //Creating 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")); ?>
Output
When the above code is executed, it will produce the following result -
1989-03-25
Example2
Let us now invoke this function passing the needed parameters -
<?php //Creating 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"); ?>
Output
When the above code is executed, it will produce the following result -
1989-03-25
Example3
Below example shows the usage of 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")); ?>
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_diff() 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.
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.