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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能