PHP UTC 到本地时间

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

PHP UTC to Local Time

phputcdst

提问by John

Server Environment

服务器环境

Redhat Enterprise Linux
PHP 5.3.5

红帽企业 Linux
PHP 5.3.5

Problem

问题

Let's say I have a UTC date and time such as 2011-04-27 02:45 and I want to convert it to my local time, which is America/New_York.

假设我有一个 UTC 日期和时间,例如 2011-04-27 02:45,我想将其转换为我的本地时间,即 America/New_York。

Three questions:

三个问题:

1.) My code below might solve the problem, would you agree?

1.) 我下面的代码可能会解决问题,你同意吗?

<?php

date_default_timezone_set('America/New_York');  // Set timezone.

$utc_ts = strtotime("2011-04-27 02:45");  // UTC Unix timestamp.

// Timezone offset in seconds. The offset for timezones west of UTC is always negative,
// and for those east of UTC is always positive.
$offset = date("Z");

$local_ts = $utc_ts + $offset;  // Local Unix timestamp. Add because $offset is negative.

$local_time = date("Y-m-d g:i A", $local_ts);  // Local time as yyyy-mm-dd h:m am/pm.

echo $local_time;  // 2011-04-26 10:45 PM

?>

2.) But, does the value of $offset automatically adjust for Daylight Savings Time (DST) ?
3.) If not, how should I tweak my code to automatically adjust for DST ?

Thank you :-)

2.) 但是,$offset 的值是否会根据夏令时 (DST) 自动调整?
3.) 如果没有,我应该如何调整我的代码以自动调整 DST ?

谢谢 :-)

回答by Treffynnon

This will do what you want using PHPs native DateTimeand DateTimeZoneclasses:

这将使用 PHP 原生DateTimeDateTimeZone类执行您想要的操作:

$utc_date = DateTime::createFromFormat(
                'Y-m-d G:i', 
                '2011-04-27 02:45', 
                new DateTimeZone('UTC')
);

$nyc_date = $utc_date;
$nyc_date->setTimeZone(new DateTimeZone('America/New_York'));

echo $nyc_date->format('Y-m-d g:i A'); // output: 2011-04-26 10:45 PM

See DateTime::createFromFormat man pagefor more information.

有关更多信息,请参阅DateTime::createFromFormat 手册页

After some experimentation between time zones that do and do not currently have DSTI have discovered that this will take DST into account. The same conversion using my method above renders the same resulting time.

当前有和没有 DST 的时区之间进行了一些实验后我发现这将考虑 DST。使用我上面的方法进行相同的转换呈现相同的结果时间。

回答by lcompare

I know this is an old post, but there is another line you need to add to get the correct time.

我知道这是一个旧帖子,但是您需要添加另一行以获得正确的时间。

Before converting to local time, you need to set the default time zone to UTC like this (if it is the time zone of the time you are providing):

在转换为本地时间之前,您需要像这样将默认时区设置为 UTC(如果它是您提供的时间的时区):

function GmtTimeToLocalTime($time) {
    date_default_timezone_set('UTC');
    $new_date = new DateTime($time);
    $new_date->setTimeZone(new DateTimeZone('America/New_York'));
    return $new_date->format("Y-m-d h:i:s");
}

回答by Ainz Ooal Gown

I'll improve on Hasin Hayder's answer

我会改进 Hasin Hayder 的回答

date_default_timezone_set('America/New_York');  // Set timezone.
$utc_ts = strtotime("2011-04-27 02:45 UTC");  // UTC Unix timestamp.
echo date('Y-m-d H:i:s a T', $utc_ts);

It should output

它应该输出

2011-04-26 10:45:00 pm EDT

The difference is adding the source Timezone. strtotime() accepts timezone too you know! :p

不同之处在于添加源时区。strtotime() 也接受时区,你知道!:p

回答by Hasin Hayder

date_default_timezone_set('America/New_York');  // Set timezone.
$utc_ts = strtotime("2011-04-27 02:45");  // UTC Unix timestamp.

Just after this executing this, $utc_ts contains the local time. PHP handles the DST itself.

在执行此操作之后, $utc_ts 包含本地时间。PHP 自己处理 DST。

=H=

=H=