Monday, 26 August 2013

PHP Function date_create()

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.

Paramters

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

No comments:

Post a Comment