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

Sr.No Parameter & Description
1

time

Optional. String in a format accepted by strtotime(), defaults to "now".

2

timezone

Optional. 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 the following result −

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

php_function_reference.htm

Advertisements