<?php
$first = '2011-04-27';
$second = '2011-04-27 16:20:12';
$third = '2011-04-27 16:20:12+00';
$dt = new DateTime($first);
$dt->setTimeZone(new DateTimeZone($dt->getTimeZone()->getName()));
echo $dt->getTimezone()->getName().'<br />';
// OK
$dt = new DateTime($second);
$dt->setTimeZone(new DateTimeZone($dt->getTimeZone()->getName()));
echo $dt->getTimezone()->getName().'<br />';
// OK
$dt = new DateTime($third);
$dt->setTimeZone(new DateTimeZone($dt->getTimeZone()->getName()));
echo $dt->getTimezone()->getName().'<br />';
// Error
// ->getName() returns unexpectedly +00, which is not a name, but offset
DateTime extracts from the last string timezone name +00
, which is really not name, but offset and therefore cannot be set as timezone...
This behavior is causing troubles when (de)serializing DateTime object...
public function __sleep()
{
$this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
return array('fix');
}
public function __wakeup()
{
$this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
unset($this->fix);
}
I know it would be better to get e.g. this string: 2011-04-27 16:20:12 UTC
, anyway as far as the metod is called getName()
, it shouldn't return +00
as a name, or should?