Mp *_*ega 3 php timezone datetime
好吧,我有以下代码
$from = "Asia/Manila";
$to = "UTC";
$org_time = new DateTime("2012-05-15 10:50:00");
$org_time = $org_time->format("Y-m-d H:i:s");
$conv_time = NULL;
$userTimezone = new DateTimeZone($from);
$gmtTimezone = new DateTimeZone($to);
$myDateTime = new DateTime($org_time, $gmtTimezone);
$offset = $userTimezone->getOffset($myDateTime);
$conv_time = date('Y-m-d H:i:s', $myDateTime->format('U') + $offset);
echo $conv_time;
Run Code Online (Sandbox Code Playgroud)
使用此代码我想转换2012-05-15 10:50:00为UTC和-8时区(我使用美国/温哥华),但它给了我一个奇怪的结果
Run Code Online (Sandbox Code Playgroud)Asia/Manila > UTC 2012-05-15 19:50:00 = the correct is 2012-05-15 02:50
而对于美国/温哥华
Asia/Manila > America/Vancouver
2012-05-16 02:50:00 = the correct is 2012-05-14 19:50
Run Code Online (Sandbox Code Playgroud)
哪里出错了?
你太过刻苦了.要在时区之间进行转换,您需要做的就是创建DateTime具有正确源时区的对象,然后通过设置目标时区setTimeZone().
$src_dt = '2012-05-15 10:50:00';
$src_tz = new DateTimeZone('Asia/Manila');
$dest_tz = new DateTimeZone('America/Vancouver');
$dt = new DateTime($src_dt, $src_tz);
$dt->setTimeZone($dest_tz);
$dest_dt = $dt->format('Y-m-d H:i:s');
Run Code Online (Sandbox Code Playgroud)