Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Date and Time Functions : date_create

date_create

Returns new DateTime object (PHP 5 >= 5.1.0)
DateTime date_create ( [string time [, DateTimeZone timezone]] )
DateTime DateTime::__construct ( [string time [, DateTimeZone timezone]] )

Parameters

time

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

timezone

Time zone of the time.

Return Values

Returns DateTime object on success or FALSE on failure.

Code Examples / Notes » date_create

jsnell

When using these functions inside of destructors or functions called as a result of being registered with register_shutdown_handler, be sure to use date_create() instead of new DateTime().  This is because new DateTime will throw an exception on failure, which is not permitted in any of the above circumstances.  If new DateTime() does fail in one of these circumstances, you will get an error stating "Fatal error: Exception thrown without a stack frame in Unknown on line 0."

karsten

The manual says "Returns DateTime object on success or FALSE on failure".
I tried hard to provoke a failure, but I seem to always get a DateTime object back, even though the PHP log says things like: "Failed to parse time string (2007W992-11:16:47+00:00) at position 5 (9): Unexpected character"
So if you (need to) check the result, beware!


dok

If you want to create the DateTime object directly from a timestamp use this
<?
$st = 1170288000 //  a timestamp
$dt = new DateTime("@$st");
?>
See also: http://bugs.php.net/bug.php?id=40171


nizar dot jouini

date_create and other DateTime related functions are included by default only in PHP versions equal and greater than 5.2.
In PHP 5.1.2 this functionality is marked to be experimental and has to be enabled at compile time.


artur

"String in a format accepted by strtotime()" is not 100% truth - you cannot pass timezone info in the string used as DateTime constructor, while you can do it with strtotime(). It may be a problem if you would like to create a date from GMT time and then display it in your local timezone, for example:
<?php
$timeZone = 'Europe/Warsaw';  // +2 hours
date_default_timezone_set($timeZone);

$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = new DateTime($dateSrc);

echo 'date(): '.date('H:i:s', strtotime($dateSrc));
// correct! date(): 14:50:00

echo 'DateTime::format(): '.$dateTime->format('H:i:s');
// INCORRECT! DateTime::format(): 12:50:00
?>
So if you want to convert date between different timezones, you have to create two DateTimeZone objects - one for the input and one for output, like this:
<?php
$timeZone = 'Europe/Warsaw';  // +2 hours
$dateSrc = '2007-04-19 12:50';

$dateTime = new DateTime($dateSrc, new DateTimeZone('GMT'));
$dateTime->setTimeZone(new DateTimeZone($timeZone));
echo 'DateTime::format(): '.$dateTime->format('H:i:s');
// CORRECT! DateTime::format(): 14:50:00
?>
I'm not sure if this is a bug or desired behaviour.


Change Language


Follow Navioo On Twitter
checkdate
date_create
date_date_set
date_default_timezone_get
date_default_timezone_set
date_format
date_isodate_set
date_modify
date_offset_get
date_parse
date_sun_info
date_sunrise
date_sunset
date_time_set
date_timezone_get
date_timezone_set
date
getdate
gettimeofday
gmdate
gmmktime
gmstrftime
idate
localtime
microtime
mktime
strftime
strptime
strtotime
time
timezone_abbreviations_list
timezone_identifiers_list
timezone_name_from_abbr
timezone_name_get
timezone_offset_get
timezone_open
timezone_transitions_get
eXTReMe Tracker