Is it possible to create DateTime object if I have some date string, and a numeric timezone value?
Something like:
$timestamp = '2007-07-07 07:07:07';
$timezone = '+0300';
$obj = new DateTime($timestamp, new DateTimeZone(????));
I need this, to add proper support for YAML-timestamps in my syck extension.
In YAML, timestamps have several representations:
- canonical: 2001-12-15T02:59:43.1Z
- valid iso8601: 2001-12-14t21:59:43.10-05:00
- space separated: 2001-12-14 21:59:43.10 -5
- no time zone (Z): 2001-12-15 2:59:43.10
- date (00:00:00Z): 2002-12-14
As you can see, in each case timezone is represented by numeric, while PHP expects me to specify timezone as a toponym…

Just use:
$dt = new DateTime( “2001-12-15T02:59:43.1Z” );
$dt = new DateTime( “2001-12-14 21:59:43.10 -5″ );
$dt = new DateTime( “2001-12-14 21:59:43.10 +0300″ );
The first argument is *not* a timestamp.
thanks, Derick… I was misleaded by comment at http://www.php.net/manual/en/function.date-create.php#74599
actually there still is a problem… but it is not related to my extension.
Try this, it should be self-descriptive
< ?php
date_default_timezone_set('GMT');
$date = "2001-12-14T21:59:43-0500";
$dt = new DateTime($date);
var_dump($dt->getTimezone()->getName());
I use this as a workaound, kind of hacky:
{{{
$offset = ‘-5′;
$date = gmdate(“Y-m-d H:i:s”, time());
$offset = (int) $offset;
// according to the wrong RFC, Etc/GMT-N means GMT+N
if ($offset > 0) {
$offset = ‘-’ . abs($offset);
} else {
$offset = ‘+’ . abs($offset);
}
$timeZoneId = ‘Etc/GMT’ . $offset;
$dateTime = new DateTime($date, new DateTimeZone(‘UTC’));
$dateTime->setTimezone(new DateTimeZone($timeZoneId));
}}}
[...] added support for timestamps in syck_load (thanks to Derick for a hint) [...]