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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_create() Function Usage and Examples

PHP Date & Time Functions Manual

The date_create() function returns a new DateTime object.

Definition and Usage

The date_create() function is an alias for DateTime::__construct() (the constructor of the DateTime class). The DateTime class represents dates and times in PHP. The date_create() function accepts a date-time string and a timezone (optional) as parameters and creates a DateTime object accordingly.

By default, this function creates the current date/Time Object

Syntax

date_create([$date_time, $timezone]);

Parameters

Serial NumberParameters and Description
1

date_time (optional)

This is the date you need to create a DateTime object for/Time string (in a supported format).

2

timezone (optional)

This indicates the timezone of the given time.

Return Value

The PHP date_create() function returns the created DateTime object.

PHP Version

This function was initially introduced in PHP version5.2is introduced in version 5.2.0 and can be used in all higher versions.

Online Example

Try the following example here, we will create a DateTime object, format it, and print the result-

<?php
   //Date String
   $date_string = "25-09-1989";
   //Create a DateTime object
   $date_time_Obj = date_create($date_string);
   //Set the date format to print the date
   $format = date_format($date_time_Obj, "Y-m-d H:i:s");
   print($format);
?>
Test to see‹/›

Output Result

1989-25-09 00:00:00

Online Example

The following example creates date and time formats separately-

<?php
   $dateString = '11-06-2012 12:50 GMT';
   $dateTime = date_create($dateString);
   print("Date: ".$dateTime->format('Y-m-d')); 
   print("\n");
   print("Time: ".$dateTime->format('H:i:s')); 
?>
Test to see‹/›

Output Result

Date: 2012-11-06
Time: 12:50:00

Online Example

The following example creates a DateTime object by specifying a date string and a timezone-

<?php
   //Date String
   $date_string = "25-09-1989, 07:32:41 GMT";
   //Create a DateTime object
   $tz = 'Asia/Shanghai';   
   $date_time_Obj = date_create($date_string, new DateTimeZone($tz));
   //Set the date format to print the date
   $format = date_format($date_time_Obj, "Y-m-d H:i:s");
   print($format);
?>
Test to see‹/›

Output Result

Array
1989-25-09 07:32:41

Online Example

In the following example, we will call the date_create() function without any parameters. It creates an object for the current time-

<?php
   //Create a DateTime object
   $date_time_Obj = date_create();
   //Set the date format to print the date
   print(date_format($date_time_Obj, "Y-m-d H:i:s
?>
Test to see‹/›

This produces the following result-

2020-04-05 12:41:31