PHP date_default_timezone_set() 东部标准时间 (EST)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5559103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:48:12  来源:igfitidea点击:

PHP date_default_timezone_set() Eastern Standard Time (EST)

phptimezonedst

提问by mrbinky3000

Long story short

长话短说

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

PHP5 是否有一个官方的、未弃用的时区,它可以识别东部标准时间——而不是东部夏令时?

Short story long :-P

长话短说:-P

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

哇,我简直不敢相信 PHP 会因为设置时间而产生这样的集群。

I would like to use PHP5's date_default_timezone_set() to set the timezone for my script. I want to use standardtime. I do notwant 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.

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

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)

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

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

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

Then there is this...

然后就是这个...

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

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

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.

上面的链接列出了我假设不推荐使用的时区。“EST”在那里,但非常以美国为中心,所以我明白为什么它被弃用了。“Ect/GMT-5”也在那里,我完全不知道为什么它被弃用了。如果你问我,所有的地名时区都应该被弃用,而 GMT 时区也应该被弃用......但是得到这个......“Ect/GMT-5”实际上是错误的!我不得不使用“Ect/GMT+5”来获得美国东部标准时间。我很确定我不会比东海岸的英格兰早 5 小时。

Answered?

回答了吗?

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"

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

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.

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

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.

我不情愿地感谢向您展示如何正确存储和检索时间的家伙,以便未来搜索此内容的人学习如何以正确的方式执行此操作。

回答by Charles

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

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

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

命名时区背后的全部原因是为了准确表示本地时间。当本地区域在夏令时时,将“标准”时间表示为本地时间在客观上是不正确的。在该时区内读取时间的任何人都会被误导。

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

如果您需要存储和使用忽略时区的日期和时间,那么为此目的使用 UTC,即存储和正常数学。值得记住的是,即使是旧的 Unix 时间戳也是“1970 年 1 月 1 日午夜后的秒数,UTC”。

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:

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

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.

没有混乱,没有大惊小怪。当我们切换时区时,它为我们处理了数学问题。



In the alternative, if you really, really, reallywant to flatten out DST timezones, look at getTransitionsand getOffsetin combination with the various timezone date()formats, Iin 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 Indianaalready.

或者,如果您真的、真的、真的想要拉平 DST 时区,请查看getTransitionsgetOffset结合各种时区date()格式I尤其是。您可以查看结果信息,以查找任何 DST 区域的“标准”版本下一次转换的时间并相应地进行调整。请记住,不同的区域在不同的时间过渡,并且并非所有区域的过渡量都相同……有些根本不会过渡。 我想有人已经提到了印第安纳州

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.

通常我也会在这里提供示例代码,但日期数学让我对暴力充满了永不满足的渴望。谁认为 60、60、24、7、4-6、12 和 52 是可以接受的思考时间和日期的方式是邪恶的,邪恶的人。谢天谢地,他们都已经死了几百到几千年。

回答by Jimmy Sawczuk

America/New_Yorkdoes observe DST and is the recommended timezone. (I went to a conference last year where Derick Rethans, who wrote a lot of the date/time code, spoke.) The idea behind the regional timezones is that they obey the timezone rules for the city/region they denote (which is why there are like 7 Indiana timezones).

America/New_York确实遵守 DST 并且是推荐的时区。(我去年参加了一个会议,编写了大量日期/时间代码的Derick Rethans发言。)区域时区背后的想法是它们遵守它们所表示的城市/区域的时区规则(这就是为什么有 7 个印第安纳时区)。

回答by steve

echo ' Format ' . date('m/d/Y H:i:s a') ;
<br/>//maketime function- mktime(hour,minute,second,month,day,year,is_dst)
<br/>$EasternTimeStamp =mktime(date('H')-6,date('i'),date('s'),date("m"),date("d"),date("Y"));
<br/>// date function- date(format, timestamp)
<br/>echo 'Format ' . date('m/d/Y H:i:s a',$EasternTimeStamp) ;

回答by Adam Hopkinson

You should always store times using UTC, which is what time()returns, then set the timezone based on the location of the server, which will be updated for DST if applicable.

您应该始终使用 UTC 存储时间,这是time()返回的时间,然后根据服务器的位置设置时区,如果适用,将针对 DST 进行更新。

回答by Matthew

Well, if you really want X hours before or after a UTC date, I would do this:

好吧,如果你真的想要在 UTC 日期之前或之后 X 小时,我会这样做:

$now = gmdate('Y-m-d H:i:s', time() - 3600 * $hours);

In your case, $hourswould be 5, I think.

在你的情况下$hours,我认为是 5。

As others have suggested, myself included, this is not good practice... But you are aware of that.

正如其他人所建议的那样,包括我自己,这不是一个好的做法......但你知道这一点。