PHP date_default_timezone_set()东部标准时间(EST) [英] PHP date_default_timezone_set() Eastern Standard Time (EST)

查看:198
本文介绍了PHP date_default_timezone_set()东部标准时间(EST)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说

PHP5是否可以将东部标准时间(不是东部夏令时)识别为正式的,不建议使用的时区?

Is there an official, un-deprecated timezone that PHP5 recognizes for Eastern STANDARD time--not Eastern DAYLIGHT time?

短篇小说:-P

哇,我不敢相信PHP会在不设置时间的情况下制造出这样的簇状菌.

Wow, I can't believe that PHP makes such a cluster-floogen out of setting the time.

我想使用PHP5的date_default_timezone_set()设置脚本的时区.我想使用标准时间.我 希望我的脚本遵守夏时制.我不想使用gmtime()并在每次程序编写时间时减去60 * 60 * 5秒.我也不想将该值保存到变量中.设置默认时区会更加优雅,并使脚本更易于移植到其他服务器和语言环境.

I would like to use PHP5's date_default_timezone_set() to set the timezone for my script. I want to use standard time. I do not want my script to observe daylight savings time. I do not want to have to use gmtime() and subtract 60*60*5 seconds each time my program writes a time. I don't want to save that value to a variable either. Setting the default timezone is more elegant and makes the script more portable to other servers and locales.

不幸的是,PHP要求我在设置默认时区时使用其愚蠢的官方时区"之一.有一个"America/New_York",但我不知道它或任何正式的PHP时区是否遵守夏令时.通过实验,我发现"America/New_York"确实遵守了DST. 美国/巴拿马"是EST,因此不遵守DST ...但是,如果巴拿马将来改变对DST的看法,该怎么办? (他们在1908年观察到DST)

Unfortunately PHP requires I use one of their stupid "official timezones" when setting the default time zone. There is an "America/New_York" but I have no idea if it or any of the official PHP timezones observe Daylight savings time. Through experimentation, I discovered that "America/New_York" does observe DST. "America/Panama" is EST and thus does not observe DST... but what if Panama should ever change their mind about DST in the future? (They observed DST in 1908)

因为巴拿马总能改变主意;我只想使用GMT或UTC偏移量.

Because Panama could always change their mind; I just want to use the GMT or UTC offset.

然后有这个...

http://us.php.net/manual/en/timezones. others.php

上面的链接列出了我所假定的时区. "EST"位于其中,但非常以美国为中心,因此我理解为什么不推荐使用它. "Ect/GMT-5"也在那里,我完全不知道为什么不赞成这样做.如果您问我,不建议使用所有地名时区,不建议使用GMT时区...但是得到这个..."Ect/GMT-5"实际上是错误的!我必须使用"Ect/GMT + 5"来获取美国的东部标准时间.我敢肯定,我不在东海岸的英国之前5小时.

The above link list what I am assuming are deprecated timezones. "EST" is in there but is very USA-centric so I understand why it is deprecated. "Ect/GMT-5" is there too and I have absolutely no idea why that is deprecated. If you ask me, all the place name timezones should be deprecated and the GMT timezones un-deprecated... but get this... "Ect/GMT-5" is actually wrong! I had to use "Ect/GMT+5" to get USA's Eastern Standard Time. I'm pretty sure I'm not 5 hours ahead of England here on the East Coast.

没有人真正回答这个问题.所以我想我会自己回答. 不,没有PHP5可以识别为标准东部时间的不弃用的时区"

No one really answered the question. So I guess I'll answer it myself. "No, there is no, un-deprecated timezone that PHP5 recognizes for Eastern STANDARD time"

很多人告诉我,当我将日期从存储中取出时,应该将日期存储为UTC时间并将其转换为本地时间.问题是,我被雇用来仅修复脚本的这一小部分,并且我怀疑这个家伙是否会让我重新编写他自PHP诞生以来一直在使用的整个代码.

A lot of folks told me I that should store dates as UTC time and translate them to local time when I pull them out of storage--something I already know. The problem is, I was hired to only fix this little part of the script and I doubt the guy is going to let me re-write his whole code which he's been using since the dawn of PHP supposedly.

我会很不情愿地向那个向您展示如何正确存储和检索时间的家伙表示感谢,以便将来搜索此内容的人们学习如何正确地进行操作.

I will reluctantly give credit to the dude who shows you how to correctly store and retrieve times so future folks searching for this learn how to do this the right way.

推荐答案

您要执行的操作不切实际,并且会导致用户讨厌您.

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

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使此 dead-simple 变得简单.在交互式提示下:

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时区,请查看 getOffset 各种时区date()格式,尤其是I.您可以戳戳和刺探结果信息,以查找下一次转换任何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 date_default_timezone_set()东部标准时间(EST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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