PHP UTC 到当地时间

发布于 2024-11-03 09:03:08 字数 886 浏览 4 评论 0 原文

服务器环境

Redhat Enterprise Linux
PHP 5.3.5

问题

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

三个问题:

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.) 但是,$offset 的值会根据夏令时 (DST) 自动调整吗?
3.)如果没有,我应该如何调整我的代码以自动调整 DST?

谢谢:-)

Server Environment

Redhat Enterprise 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.

Three questions:

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

<?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 :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怀里藏娇 2024-11-10 09:03:08

这将使用 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

参见 DateTime::createFromFormat man页面了解更多信息。

经过一些实验 目前有和没有 DST 的时区 我发现这会考虑 DST。使用我上面的方法进行相同的转换会产生相同的结果时间。

This will do what you want using PHPs native DateTime and DateTimeZone classes:

$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.

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.

离不开的别离 2024-11-10 09:03:08

我知道这是一篇旧文章,但您需要添加另一行才能获得正确的时间。

在转换为本地时间之前,您需要将默认时区设置为 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 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):

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");
}
乖乖 2024-11-10 09:03:08
date_default_timezone_set('America/New_York');  // Set timezone.
$utc_ts = strtotime("2011-04-27 02:45");  // UTC Unix timestamp.

执行此命令后,$utc_ts 包含本地时间。 PHP 自行处理 DST。

=H=

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.

=H=

掐死时间 2024-11-10 09:03:08

我将改进 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);

它应该输出

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

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

I'll improve on Hasin Hayder's answer

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

风筝在阴天搁浅。 2024-11-10 09:03:08
<?php
    $time = new DateTime('now', new DateTimeZone(date_default_timezone_get()));
    $timeZone = $time->format('P');//Asia/Kolkata ... GET TimeZone From PHP ini Setting
    $tm_datetime = '08/08/2021 12:00 AM';
    $tm_tz_from = $timeZone;
    $tm_tz_to = 'UTC';
    $tm_format = 'Ymd\THis\Z';
    $dt = new DateTime($tm_datetime, new DateTimeZone($tm_tz_from));
    $dt->setTimeZone(new DateTimeZone($tm_tz_to));
    $utc_time_from =$dt->format("H:i:s");
    echo "UTC TIME".$utc_time_from;

?>
<?php
    $time = new DateTime('now', new DateTimeZone(date_default_timezone_get()));
    $timeZone = $time->format('P');//Asia/Kolkata ... GET TimeZone From PHP ini Setting
    $tm_datetime = '08/08/2021 12:00 AM';
    $tm_tz_from = $timeZone;
    $tm_tz_to = 'UTC';
    $tm_format = 'Ymd\THis\Z';
    $dt = new DateTime($tm_datetime, new DateTimeZone($tm_tz_from));
    $dt->setTimeZone(new DateTimeZone($tm_tz_to));
    $utc_time_from =$dt->format("H:i:s");
    echo "UTC TIME".$utc_time_from;

?>
时光与爱终年不遇 2024-11-10 09:03:08

我不喜欢更改默认时区的答案。根据我在夏令时变化的国家生活的经验,正确的方法是始终使用 UTC,然后在向用户显示时更改为当地时间。

这是我使用的,从一个时区到另一个时区(在本例中从 UTC 到欧洲/马德里)。

$newDateTime = new DateTime($sourcetime, new DateTimeZone("UTC")); 
$newDateTime->setTimezone(new DateTimeZone("Europe/Madrid")); 
echo $newDateTime->format("Y-m-d H:i:s")." ";

如果您需要日期之间的秒数操作,请使用 $newDateTime->getTimestamp(),例如:

$difftime=time()-$newDateTime->getTimestamp();

I dislike the answers that changes default timezone. The right way based in my experience living in a country with summer time change is to work always in UTC and then, when showing to user, change to localtime.

This is the one that I use, from one timezone to another (In this example from UTC to Europe/Madrid).

$newDateTime = new DateTime($sourcetime, new DateTimeZone("UTC")); 
$newDateTime->setTimezone(new DateTimeZone("Europe/Madrid")); 
echo $newDateTime->format("Y-m-d H:i:s")." ";

if you need the seconds for operation between dates then use $newDateTime->getTimestamp(), For example:

$difftime=time()-$newDateTime->getTimestamp();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文