Today I spent way too much time figuring out how to convert dates and times to different time zones. PHP can barely do it, and WordPress really doesn’t do anything to make it easier. I would love it if someone would just create a simple function like this:
$string = format_gmt_time_for_time_zone(
'l, F jS g:ia',
$myGMTTimestamp,
'America/Los Angeles');
or even better, using and object oriented approach:
$string = $date->Format(
'l, F jS g:ia',
'America/Los Angeles');
It’s important because it’s kind of a “best practice” to store dates in the database in GMT (or really UTC) format and then translate them as necessary. Then I want someone in New York to see times in their time zone and also someone in Seattle. It would also help if WordPress had a per user time zone.
I know that PHP 6 is doing some enhanced date stuff, so hopefully that will include this simple functionality. But right now, both WordPress and PHP don’t seem to imaging a case where someone wants to print out a time for a timezone they’re not in.
What I ended up doing was this:
$oldTimeZone = date_default_timezone_get();
date_default_timezone_set($requestedTimeZone));
$convertedString = date($formatString, $gmtTimestamp);
date_default_timezone_set($oldTimeZone);
return $convertedString;
It works, but it’s fairly clunky. From my experience at Microsoft I know sometimes when there’s a whole category of things that needs improving, it can be hard to find someone to take that on. What has to happen is a person needs to say, “I’m going to take on times and dates and make the API better.” But either people aren’t interesting or people are too afraid to suggest such a drastic move. Furthermore, the people who wrote the original code will often be offended that someone would suggest it could be better. They will point to something like my code above an say that’s good enough. It isn’t.
I’m actually considering getting involved with WordPress on this, however, I am honestly scared that they will not be into it. I’d also like to add the per user time zone, but I sense something like that would be controversial.
Submit a Comment