【发布时间】:2014-10-22 21:44:04
【问题描述】:
以下 PHP 代码:
function serverTimeZone_offset($userTimeZone)
{
$userDateTimeZone = new DateTimeZone($userTimeZone);
$userDateTime = new DateTime("now", $userDateTimeZone);
$serverTimeZone = date_default_timezone_get();
$serverDateTimeZone = new DateTimeZone($serverTimeZone);
$serverDateTime = new DateTime("now", $serverDateTimeZone);
return $serverDateTimeZone->getOffset($userDateTime);
}
function getDefineTimeZone($timezone)
{
$userDateTimeZone = new DateTimeZone($timezone);
return new DateTime("now", $userDateTimeZone);
}
function getServerTimeZone()
{
$serverTimeZone = date_default_timezone_get();
$serverDateTimeZone = new DateTimeZone($serverTimeZone);
return new DateTime("now", $serverDateTimeZone);
}
$userDateTime = getDefineTimeZone('America/Curacao');
$serverDateTime = getServerTimeZone();
$timeOffset = serverTimeZone_offset('America/Curacao');
var_dump($userDateTime);
var_dump($serverDateTime);
var_dump($timeOffset); // the seconds is incorrect ?!?!
// adding the timezone difference
$userDateTime->add(new DateInterval('PT'.$timeOffset.'S'));
var_dump($userDateTime);
将输出:
object(DateTime)[2]
public 'date' => string '2014-10-22 17:36:39' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Curacao' (length=15)
object(DateTime)[3]
public 'date' => string '2014-10-22 23:36:39' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)
int 7200
object(DateTime)[2]
public 'date' => string '2014-10-22 19:36:39' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Curacao' (length=15)
这显然是不正确的。偏移量返回 7200 秒(仅 2 小时)而不是 21600 秒(6 小时)。为什么?
【问题讨论】:
标签: php datetime time timezone timezone-offset