如何在 php 中从 GMT+0 检索时区偏移量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9140765/
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-26 06:14:51  来源:igfitidea点击:

How to retrieve timezone offset from GMT+0 in php?

phptimezone-offset

提问by Jér?me Verstrynge

I am no expert in php. I know timezones are supportedin PHP.

我不是 php 专家。我知道PHP支持时区。

For a given timezone TZ supported by PHP, I need to retrieve the offset (i.e., number of hours and minutes to add or substract) to a known UTC (i.e. GMT+0) to get that time in the TZ zone.

对于 PHP 支持的给定时区 TZ,我需要检索已知 UTC(即 GMT+0)的偏移量(即添加或减去的小时数和分钟数)以获取 TZ 区域中的时间。

How can I achieve this? Ultimately, I need to get those offsets for all supported timezones in PHP. Thanks.

我怎样才能做到这一点?最终,我需要为 PHP 中所有支持的时区获取这些偏移量。谢谢。

回答by Teneff

This is a simple example how to get timezone offset in seconds:

这是一个如何以秒为单位获取时区偏移的简单示例:

$dtz = new DateTimeZone('Europe/Sofia');
$time_in_sofia = new DateTime('now', $dtz);
echo $dtz->getOffset( $time_in_sofia );

to display it in the format GMT+x:

以 GMT+x 格式显示:

$offset = $dtz->getOffset( $time_in_sofia ) / 3600;
echo "GMT" . ($offset < 0 ? $offset : "+".$offset);

working example

工作示例

回答by Sundar

This will be useful for converting offset values into GMT format without any calculation

这对于将偏移值转换为 GMT 格式而不进行任何计算很有用

<?php

  //target time zone
  $target_time_zone = new DateTimeZone('America/Los_Angeles');

  //find kolkata time 
  $date_time = new DateTime('now', $target_time_zone);

  //get the exact GMT format
  echo 'GMT '.$date_time->format('P');

回答by Alix Axel

This will get you the standard offset (not the one in orout of DST[depending on the time of year]):

这将为您提供标准偏移量(不是DST外的偏移量[取决于一年中的时间]):

function getStandardOffsetUTC($timezone)
{
    if($timezone == 'UTC') {
        return '';
    } else {
        $timezone = new DateTimeZone($timezone);
        $transitions = array_slice($timezone->getTransitions(), -3, null, true);

        foreach (array_reverse($transitions, true) as $transition)
        {
            if ($transition['isdst'] == 1)
            {
                continue;
            }

            return sprintf('UTC %+03d:%02u', $transition['offset'] / 3600, abs($transition['offset']) % 3600 / 60);
        }

        return false;
    }
}

Usage:

用法:

echo getStandardOffsetUTC('Europe/Sofia'); // UTC +02:00

You can get a peek at the differences here.

您可以在此处查看差异。

回答by MNR

It is possible just by the following single line.

只需通过以下单行即可。

echo (new DateTime('now', new DateTimeZone( 'Asia/Kabul' )))->format('P');

回答by J. Bruni

For all supported timezones in PHP:

对于 PHP 中所有支持的时区:

$timezone_offsets = array();

foreach(timezone_identifiers_list() as $timezone_identifier)
{
    $date_time_zone = new DateTimeZone($timezone_identifier);
    $date_time = new DateTime('now', $date_time_zone);
    $timezone_offsets[$timezone_identifier] = $date_time_zone->getOffset($date_time);
}

print_r($timezone_offsets);