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