简体   繁体   English

PHP UTC 到本地时间

[英]PHP UTC to Local Time

Server Environment服务器环境

Redhat Enterprise Linux红帽企业 Linux
PHP 5.3.5 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)? 2.) 但是,$offset 的值是否会根据夏令时 (DST) 自动调整?
3.) If not, how should I tweak my code to automatically adjust for DST? 3.)如果没有,我应该如何调整我的代码以自动调整 DST?

Thank you:-)谢谢:-)

This will do what you want using PHPs native DateTime and DateTimeZone classes:这将使用 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 page for more information.有关更多信息,请参阅DateTime::createFromFormat 手册页

After some experimentation between time zones that do and do not currently have DST I have discovered that this will take DST into account.在有和没有夏令时的时区之间进行了一些实验后,我发现这将考虑夏令时。 The same conversion using my method above renders the same resulting time.使用我上面的方法进行相同的转换会产生相同的结果时间。

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");
}

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应该是 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

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.就在执行此操作之后,$utc_ts 包含本地时间。 PHP handles the DST itself. PHP 自己处理 DST。

=H= =H=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM