PHP 中的 UTC 偏移量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/193499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:53:19  来源:igfitidea点击:

UTC Offset in PHP

phptimezoneutc

提问by Adam Ernst

What's the easiest way to get the UTC offset in PHP, relative to the current (system) timezone?

相对于当前(系统)时区,在 PHP 中获取 UTC 偏移量的最简单方法是什么?

回答by Czimi

  date('Z');

returns the UTC offset in seconds.

以秒为单位返回 UTC 偏移量。

回答by Tuhin Bepari

// will output something like +02:00 or -04:00
echo date('P');

回答by John Millikin

timezone_offset_get()

timezone_offset_get()

$this_tz_str = date_default_timezone_get();
$this_tz = new DateTimeZone($this_tz_str);
$now = new DateTime("now", $this_tz);
$offset = $this_tz->getOffset($now);

Untested, but should work

未经测试,但应该工作

回答by Kenny

I did a slightly modified version of what Oscar did.

我对奥斯卡所做的做了一个稍微修改的版本。

date_default_timezone_set('America/New_York');
$utc_offset =  date('Z') / 3600;

This gave me the offset from my timezone, EST, to UTC, in hours.

这给了我从我的时区 EST 到 UTC 的以小时为单位的偏移量。

The value of $utc_offset was -4.

$utc_offset 的值为 -4。

回答by HMagdy

Simply you can do this:

你可以这样做:

//Object oriented style
function getUTCOffset_OOP($timezone)
{
    $current   = timezone_open($timezone);
    $utcTime  = new \DateTime('now', new \DateTimeZone('UTC'));
    $offsetInSecs =  $current->getOffset($utcTime);
    $hoursAndSec = gmdate('H:i', abs($offsetInSecs));
    return stripos($offsetInSecs, '-') === false ? "+{$hoursAndSec}" : "-{$hoursAndSec}";
}

//Procedural style
function getUTCOffset($timezone)
{
    $current   = timezone_open($timezone);
    $utcTime  = new \DateTime('now', new \DateTimeZone('UTC'));
    $offsetInSecs =  timezone_offset_get( $current, $utcTime);
    $hoursAndSec = gmdate('H:i', abs($offsetInSecs));
    return stripos($offsetInSecs, '-') === false ? "+{$hoursAndSec}" : "-{$hoursAndSec}";
}


$timezone = 'America/Mexico_City';

echo "Procedural style<br>";
echo getUTCOffset($timezone); //-06:00
echo "<br>";
echo "(UTC " . getUTCOffset($timezone) . ") " . $timezone; // (UTC -06:00) America/Mexico_City
echo "<br>--------------<br>";
echo "Object oriented style<br>";
echo getUTCOffset_OOP($timezone); //-06:00
echo "<br>";
echo "(UTC " . getUTCOffset_OOP($timezone) . ") " . $timezone; // (UTC -06:00) America/Mexico_City

回答by ????

This is same JavaScript date.getTimezoneOffset()function:

这是相同的 JavaScriptdate.getTimezoneOffset()函数:

<?php
echo date('Z')/-60;
?>

回答by Amr

date("Z")will return the UTC offset relative to the server timezone not the user's machine timezone. To get the user's machine timezone you could use the javascript getTimezoneOffset()function which returns the time difference between UTC time and local time, in minutes.

date("Z")将返回相对于服务器时区而不是用户机器时区的 UTC 偏移量。要获取用户的机器时区,您可以使用 javascriptgetTimezoneOffset()函数返回 UTC 时间和本地时间之间的时差,以分钟为单位。

<script type="text/javascript">
    d = new Date();
    window.location.href = "page.php?offset=" + d.getTimezoneOffset();
</script>

And in page.phpwhich holds your php code, you can do whatever you want with that offset value. Or instead of redirecting to another page, you can send the offset value to your php script through Ajax, according to your needs.

page.php其中保存您的 php 代码,您可以使用该偏移值做任何您想做的事情。或者,您可以根据需要通过 Ajax 将偏移值发送到您的 php 脚本,而不是重定向到另一个页面。