反转时区偏移量

时间:2010-05-23 06:26:34

标签: php datetime timezone

我有一个时区名称,我希望时区的名称加倍其偏移量。

例如,亚洲/迪拜是+4,我想将其反转为-4 ......并将其解决为EDT

语言:PHP

以下是它的样子:

$timezone = "Asia/Dubai"
$offset = $timezone->getOffset();
$offset 0 - $offset;
$timezone = $offset->getTimeZone();

输出:EDT

2 个答案:

答案 0 :(得分:0)

(PHP 5> = 5.2.0)

获取UTC的偏移量

<?php
$offUTC = new DateTime('now', new DateTimeZone('Asia/Dubai'));

echo $offUTC->getOffset(); // in seconds
echo ($offUTC->getOffset()/3600); // in hours
?>

获取两个任意区域之间的时差

<?php
$timezoneDubai = new DateTimeZone("Asia/Dubai");
$timezoneEDT = new DateTimeZone("America/New_York"); // note EDT may work, support is not clear

$datetimeDubai= new DateTime("now", $timezoneDubai);
$datetimeEDT = new DateTime("now", $timezoneEDT);

$timeOffset = $datetimeEDT->getOffset($datetimeDubai);

echo $timeOffset; // in seconds
echo ($timeOffset/3600) // in hours
?>

我不是100%确定你的目标是什么,但这应该对你有所帮助。也可以在这里查看更多DateTime函数:http://www.php.net/manual/en/book.datetime.php

答案 1 :(得分:0)

$timezone = "Asia/Dubai";
$timezone = new \DateTimeZone($timezone);
$dateUTC = new \DateTime('now', new \DateTimeZone('UTC'));
// Offset from UTC in seconds
$offset = $timezone->getOffset($dateUTC);
// Reverse offset and convert to hours
$timezone = new \DateTimeZone($offset * -1 / 60 / 60);
echo $timezone->getName();