我需要在PHP中获得两个时区之间的偏移量。我使用下面的代码来做到这一点:
$origin_tz="America/Los_Angeles";
$remote_tz="UTC";
$origin_dtz = new DateTimeZone($origin_tz);
$remote_dtz = new DateTimeZone($remote_tz);
$origin_dt = new DateTime("now", $origin_dtz);
$remote_dt = new DateTime("now", $remote_dtz);
$offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
我的偏移为0.我在这里做错了什么?
答案 0 :(得分:0)
getOffset:返回UTC的时区偏移量(以秒为单位)
$origin_tz="America/Los_Angeles";
$remote_tz="Europe/Berlin";
$origin_dtz = new DateTime('NOW', new DateTimeZone($origin_tz));
$remote_dtz = new DateTime('NOW', new DateTimeZone($remote_tz));
$originOffset = $origin_dtz->getOffset(); // -25200 => 7h to UTC
$remoteOffset = $remote_dtz->getOffset(); // 7200 => 2h to UTC
$offset = $originOffset - $remoteOffset; // -32400 => -9h