PHP - Function date_create()


Advertisements


Syntax

DateTime date_create ( [$time [, timezone]] );

DateTime DateTime::__construct ( [$time [, $timezone]] );

Definition and Usage

These functions return new DateTime object.

The above two functions are equivalent and any of the functions can be used as shown below in the example.

Parameters

ParameterDescription
timeOptional. String in a format accepted by strtotime(), defaults to "now".
timezoneOptional. Time zone of the time.

Return Value

Returns DateTime object on success or FALSE on failure.

Example

Following is the usage of this function:

<?php
   $dateSrc = '2007-04-19 12:50 GMT';
   $dateTime = new DateTime($dateSrc);
   echo 'DateTime::format(): '.$dateTime->format('H:i:s');
   echo "\n";

   $dateSrc = '2007-04-19 12:50 GMT';
   $dateTime = date_create($dateSrc);
   echo 'DateTime::format(): '.$dateTime->format('H:i:s');
?> 

This will produce following result:

DateTime::format(): 12:50:00
DateTime::format(): 12:50:00

php_function_reference.htm

Advertisements