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

查看:71
本文介绍了在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时间戳也是"UTC 1970年1月1日午夜之后的秒数".

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时区,请结合各种时区date()格式(特别是I)查看getTransitions和getOffset.您可以戳戳和刺探结果信息,以查找下一次转换任何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.

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

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