Time Zones and DST and arbitrary formats
Posted: Wed Dec 07, 2005 5:21 pm
I have been reading the user comments in gmdate() and date() and have come to the conclusion that I am totally confused. The people offer lots of ideas for how to adjust time zones before outputting a time stamp via (gm)date(). The general consensus is manually adjust the time to local by +-3600*n and then outputting via gmdate(). It's a hacky trick, but it seems to work.
So, now the trouble is translating a timezone key like EST to a numeric offset (i.e. -5). There are many "special" cases and though PHP seems to have knowledge of these time zones, I'm at loss how to access them. Perhaps it's not worth the trouble: just have them enter numeric offsets?
date("I") appears to work for detecting daylight savings time, but once again, there are many variations across the globe for daylight savings time. Without a lookup table, is it necessary to simply forgo daylight savings and have the user switch it on or off? Or are daylight saving time beginnings and endings uniform enough to just ask the user whether or not to automatically detect DST? Perhaps all two options should be allowed?
I realize I have asked this question before. But I would like to not have to constantly change my time preferences whenever something funky happens (ex. DST on/off). If this is impossible, tell me so, and I'll abandon this idea.
Mediawiki, which runs the extremely international Wikipedia, uses offsets like -5:00. I assume they parse the string into a negative, a 5, and a 0, which can be used to determine seconds without tricky floats. You have to adjust your time when DST kicks in. It uses this code to guess offsets:
Finally, when it comes to actually parsing timestamps into something more readable, are there any inherent security risks in allowing users to pass arbitrary strings to the gmdate() function? If so, what escaping/filtering should be done to mitigate these risks?
So, now the trouble is translating a timezone key like EST to a numeric offset (i.e. -5). There are many "special" cases and though PHP seems to have knowledge of these time zones, I'm at loss how to access them. Perhaps it's not worth the trouble: just have them enter numeric offsets?
date("I") appears to work for detecting daylight savings time, but once again, there are many variations across the globe for daylight savings time. Without a lookup table, is it necessary to simply forgo daylight savings and have the user switch it on or off? Or are daylight saving time beginnings and endings uniform enough to just ask the user whether or not to automatically detect DST? Perhaps all two options should be allowed?
I realize I have asked this question before. But I would like to not have to constantly change my time preferences whenever something funky happens (ex. DST on/off). If this is impossible, tell me so, and I'll abandon this idea.
Mediawiki, which runs the extremely international Wikipedia, uses offsets like -5:00. I assume they parse the string into a negative, a 5, and a 0, which can be used to determine seconds without tricky floats. You have to adjust your time when DST kicks in. It uses this code to guess offsets:
Code: Select all
// in [-]HH:MM format...
// won't yet work with non-even tzs
function fetchTimezone() {
// FIXME: work around Safari bug
var localclock = new Date();
// returns negative offset from GMT in minutes
var tzRaw = localclock.getTimezoneOffset();
var tzHour = Math.floor( Math.abs(tzRaw) / 60);
var tzMin = Math.abs(tzRaw) % 60;
var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
":" + ((tzMin < 10) ? "0" : "") + tzMin;
return tzString;
}