DateTime vs numeric timezone

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…

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • FriendFeed
  • Reddit
  • Tumblr
  • Twitter
Liked this post? Follow me on twitter:@jimi_dini.

  • http://derickrethans.nl Derick

    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.

  • http://indeyets.pp.ru/ indeyets

    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());

  • epaulin

    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));

    }}}

  • http://blog.milkfarmsoft.com/?p=62 Alexey Zakhlestin’s blog »Syck for PHP,0.9.1 (aka “beta2″)

    [...] added support for timestamps in syck_load (thanks to Derick for a hint) [...]

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.