PHP date_default_timezone_set() Eastern Standard Time (EST)

后端 未结 5 694
梦谈多话
梦谈多话 2020-12-17 16:48

Long story short

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

相关标签:
5条回答
  • 2020-12-17 17:30

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

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

    In your case, $hours would be 5, I think.

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

    0 讨论(0)
  • 2020-12-17 17:32
    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) ;
    
    0 讨论(0)
  • 2020-12-17 17:36

    America/New_York does 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).

    0 讨论(0)
  • 2020-12-17 17:38

    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.

    0 讨论(0)
  • 2020-12-17 17:39

    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.

    0 讨论(0)
提交回复
热议问题