doucang9673 2011-04-05 22:02
浏览 160
已采纳

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

Long story short

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

Short story long :-P

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

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.

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)

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

Then there is this...

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.

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"

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.

  • 写回答

5条回答 默认 最新

  • dpiuqwwyu187975836 2011-04-05 22:16
    关注

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

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

    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.


    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog