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() Function.
The PHP date_create() function is an alias of the built-in PHP DateTime::_construct, a constructor of the DateTime class. Where, DateTime class represents date and time in PHP. This PHP function accepts a date time string and time zone as parameters and create a DateTime object accordingly.
By default, this function creates an object of the current date/time.
The PHP date_create() function is an alias of the built-in PHP DateTime::_construct, a constructor of the DateTime class. Where, DateTime class represents date and time in PHP. This PHP function accepts a date time string and time zone as parameters and create a DateTime object accordingly.
By default, this function creates an object of the current date/time.
Syntax
Following below is the syntax to use this function -
date_create([$date_time, $timezone]);
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | date_time (Optional) This is the date/time string (in supported formats) for which you need to create a DateTime object. |
2 | timezone (Optional) This represents the timezone of the given time. |
Return Value
It returns the created DateTime object.
PHP Version
This function was first introduced as part of core PHP v 5.2.0 and, works with all the later versions.
Example1
Try the following example. Here, we are creating a DateTime object, formatting it, and then printing the result to the screen -
<?php //Date string $date_string = "25-09-1989"; //Creating a DateTime object $date_time_Obj = date_create($date_string); //formatting the date to print it $format = date_format($date_time_Obj, "d-m-Y H:i:s"); print($format); ?>
Output
When the above code is executed, it will produce the following result -
25-09-1989 00:00:00
Example2
Following example below creates a date, formats it as date and time separately -
<?php $dateString = '11-06-2012 12:50 GMT'; $dateTime = date_create($dateString); print("Date: ".$dateTime->format('d-m-y')); print("\n"); print("Time: ".$dateTime->format('H:i:s')); ?>
Output
When the above code is executed, it will produce the following result -
Date: 11-06-12 Time: 12:50:00
Example3
The following example creates a DateTime object specifying both date string and time zone -
<?php //Date string $date_string = "25-09-1989, 07:32:41 GMT"; //Creating a DateTime object $tz = 'Indian/Mahe'; $date_time_Obj = date_create($date_string, new DateTimeZone($tz)); //formatting the date to print it $format = date_format($date_time_Obj, "d-m-y H:i:s"); print($format); ?>
Output
When the above code is executed, it will produce the following result -
Array 25-09-89 07:32:41
Example4
In the following example, we are invoking the built-in date_create() function without any parameters. This creates the object of the current time -
<?php //Creating a DateTime object $date_time_Obj = date_create(); //formatting the date to print it print(date_format($date_time_Obj, "d-m-y H:i:s")); ?>
Output
When the above code is executed, it will produce the following result -
04-05-20 12:41:31
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_date_set() Function.
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.
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.