PHP - DateTime - 在ET中显示时间,而不是EST或EDT [英] PHP - DateTime - show time in ET instead of EST or EDT

查看:202
本文介绍了PHP - DateTime - 在ET中显示时间,而不是EST或EDT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小片段,用于格式化这样的时间戳的日期:(YYYY-MM-DD H:M:ST)例如:2011-09-29 17:00:46 EST或2011-09-29 17:00:46 EDT

I have a little snippet that formats a date for a timestamp like this: (YYYY-MM-DD H:M:S T ) Ex: 2011-09-29 17:00:46 EST or 2011-09-29 17:00:46 EDT

我想将EST和EDT转换为ET并显示ET,所以用户不要担心Dayligt的储蓄。如何做?

I would like convert EST and EDT to ET and show ET instead, so the users don't bother about Dayligt savings. How do I do that?

    $timeZone = new DateTimeZone('America/New_York');
    $processedDate = str_replace(".000000", "", $timestamp);
    $dateTime = new DateTime($processedDate, new DateTimeZone('GMT'));
    $dateTime->setTimezone($timeZone);
    $processedDate = $dateTime->format('Y-m-d H:i:s') . ' ' . $dateTime->format('T');
    echo processedDate;

谢谢

推荐答案

你想做的事情是不实际的,会导致你的用户讨厌你。

The thing that you want to do is not practical and will lead to your users hating you.

命名时区背后的所有原因是准确的代表当地时间。将标准时间表示为当地区域在DST内的本地时间是客观上不正确的。任何读取时间的人将被误导。

The entire reason behind the named time zones is to accurately represent local time. Representing "Standard" time as a local time when that local area is inside DST is objectively incorrect. Anyone inside that time zone that reads the time is going to be misinformed.

如果您需要存储和处理忽略时区的日期和时间,请使用UTC那个目的,即存储和正常的数学。值得记住的是,即使是很好的旧Unix时间戳也是从1970年1月1日午夜到UTC的UTC秒。

If you need to store and work with dates and times that ignore time zones, then use UTC for that purpose, i.e. storage and normal math. It's worth remembering that even the good old Unix timestamp is "seconds past midnight, January 1, 1970, UTC".

如果你需要代表用户的本地时间,那么你应该允许他们选择他们当地的时区,并在显示时转换它。现代PHP的DateTime和DateTimeZone使这个非常简单。从交互式提示:

If you need to represent times local to the user, then you should allow them to pick their local time zone, and convert it on display. Modern PHP's DateTime and DateTimeZone make this dead-simple. From the interactive prompt:

php > $utc = new DateTimeZone('UTC');
php > $amla = new DateTimeZone('America/Los_Angeles');
php >
php > $two_past_midnight = new DateTime('2011-04-05 00:02', $utc);
php > $two_past_midnight->setTimeZone($amla);
php > echo $two_past_midnight->format('Y-m-d H:i:s');
2011-04-04 17:02:00

没有混乱,没有大惊小怪。当我们切换时区时,它照顾了我们的数学。

No mess, no fuss. It took care of the math for us when we switched the time zone.

在另一种选择中,如果你真的,真的想要平铺DST时区,看看getTransitions和getOffset结合各种时区date()格式,特别是我。您可以对所得到的信息进行戳和生成,以便在任何DST区域的标准版本下一步转换并相应地进行调整时查找。请记住,不同的地区在不同的时间过渡,并不是所有的地区都以相同的数量过渡...而且有些不会过渡。我想有人提到了印第安纳州。

In the alternative, if you really, really, really want to flatten out DST timezones, look at getTransitions and getOffset in combination with the various timezone date() formats, I in particular. You can poke and prod at the resulting information to find when the "standard" version of any DST zone next transitions and adjust it accordingly. Remember that different areas transition at different times, and not all areas transition by the same amount ... and some don't transition at all. I think someone mentioned Indiana already.

通常我也会在这里提供示例代码,但日期数学填补了对暴力的绝望渴望。无论谁决定,60,60,24,7,4-6,12和52是可以接受的方式来思考时间和日期是邪恶,邪恶的人。幸运的是,他们已经死了几十万年。

Normally I'd also provide sample code here, but date math fills me with an insatiable thirst for violence. Whoever decided that 60, 60, 24, 7, 4-6, 12 and 52 were acceptable ways to think about times and dates were evil, evil people. Thankfully they've all been dead for between hundreds and thousands of years.

这篇关于PHP - DateTime - 在ET中显示时间,而不是EST或EDT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆