"The $timezone parameter and the current timezone are ignored when the $time parameter [�_�] is a UNIX timestamp."
Watch out �_" this means that these two are NOT equivalent, they result in different timezones (unless your current timezone is GMT):
<?php
$d = new DateTime(); $d->setTimestamp($t);
echo $o->format('O');
// +0200
$d = new DateTime('@' . $t);
echo $o->format('O');
// +0000
?>
DateTime::__construct
(PHP 5 >= 5.2.0)
DateTime::__construct — Returns new DateTime object
Описание
Returns new DateTime object.
Список параметров
- time
-
String in a format accepted by strtotime(), defaults to "now".
- timezone
-
Time zone of the time.
Errors/Exceptions
Emits Exception in case of an error.
Примеры
Пример #1 DateTime::__construct() example
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2008-08-03 14:52:10');
echo $datetime->format(DATE_ATOM);
?>
User Contributed Notes
DateTime::__construct
DateTime::__construct
Tim Strehle
05-May-2010 10:50
05-May-2010 10:50
kendsnyder at gmail dot com
12-Jan-2010 04:37
12-Jan-2010 04:37
Also forgot to mention, that MySQL "zeroed" dates do not throw an error but produce a non-sensical date:
<?php
$d = new DateTime("0000-00-00");
$d->format("Y-m-d"); // "-0001-11-30"
?>
Another good reason to write your own class that extends from DateTime.
kendsnyder at gmail dot com
12-Jan-2010 04:24
12-Jan-2010 04:24
The theoretical limits of the date range seem to be "-9999-01-01" through "9999-12-31" (PHP 5.2.9 on Windows Vista 64):
<?php
$d = new DateTime("9999-12-31");
$d->format("Y-m-d"); // "9999-12-31"
$d = new DateTime("0000-12-31");
$d->format("Y-m-d"); // "0000-12-31"
$d = new DateTime("-9999-12-31");
$d->format("Y-m-d"); // "-9999-12-31"
?>
Dates above 10000 and below -10000 do not throw errors but produce weird results:
<?php
$d = new DateTime("10019-01-01");
$d->format("Y-m-d"); // "2009-01-01"
$d = new DateTime("10009-01-01");
$d->format("Y-m-d"); // "2009-01-01"
$d = new DateTime("-10019-01-01");
$d->format("Y-m-d"); // "2009-01-01"
?>