Sun 29 Apr 2018 8:29AM

WordPress and time zones

BW Bob W Public Seen by 283

A thread to talk about time zone management on the preVH


Bob W Sun 29 Apr 2018 8:32AM

I've started looking into how to manage time zones on a WordPress site, and am appalled to seemingly discover that it probably can't be done, at least in a reliably correct and sane way.

The behavior I would think one would want is that each user would see all times displayed in their own local time zone. Furthermore, you'd like things to automatically adjust for daylight savings / summertime time changes.

But, it appears that the architecture of WordPress is so messed up that things are unlikely to ever work as above. For one thing, it seems like things are set up so WP expects you to just set a UTC offset -- how many hours to adjust relative to UTC, which means it's impossible to reflect seasonal time changes. For another thing, there is an "obvious" way to set a time zone in PHP -- and some plugins actually use this method -- but it turns out that breaks other things, because WP expects you to never use that method, and to always keep the PHP master time zone set at UTC.

There are a few time zone related plugin out there. The one that most looks like it might do what I would like is obsolete and possibly broken. (Hmm. Can't find it now. There is another one that isn't as good a match but might slightly less broken?) Some of the other ones that look vaguely helpful say that they work by doing the thing that other documentation claims one should never do.

I guess in the near term we will need to just set the site's master time zone to UTC (to be as geographically neutral as possible) and display all times in UTC. Then, maybe I'll build my own plug that does what I want (though the architecture doesn't make it easy, and it might never really do what I would like?)


Julie Sun 29 Apr 2018 8:50PM

Bob, are you meaning for Events (in a calendar)? Or for other things as well (and if so, what)? I know we're wanting to avoid Google in general, but wouldn't embedding a google calendar handle time zones for us? Similarly, I thought event plugins (like Event Manager or The Event Calendar) do the same ...


Brent Barker Sun 29 Apr 2018 8:58PM

I just looked at the Google Calendar for the NFP, and it looks like it does not change to one's local timezone - the calendar has its own timezone setting and everything reflects that.



Julie Sun 29 Apr 2018 9:00PM

Damn!! I was thinking of when you send an invite from Google Calendar, then it changes to the person's timezone ... I had assumed it would do so online also :(


Bob W Sun 29 Apr 2018 9:54PM

Ideally, I'd love all times displayed on the site to be displayed in the user's preferred timezone. If we can't have that for everything, well, localization is more important for some times than others. Times for events, as on a calendar, are particularly handy to have in one's local time.

The WP standard is to have a timezone at a site level, not a user level (and the site level timezone is set in a way that requires manual adjustment for seasonal time changes, apparently as a legacy of some screwed up limitations on the host of one of the WP creators). So, it's the site timezone that I would expect plugins to honor; but there is no standard mechanism for a user-level timezone.

I imagine that as soon as I get a little more familiar with WP, I'll be able to do some custom coding to coerce reasonable behavior, at least for the cases we most care about (i.e., events). But, I'm skeptical of all the out-of-the-box solutions I've seen so far.


Pan Vera Sun 29 Apr 2018 9:15PM


How about this addin?

This plugin retrieves the time zone of the client using “Automatic Timezone Detection Using JavaScript” (http://pellepim.bitbucket.org/jstz/) the first time that a client accesses the site, sets a cookie containing the client’s time zone on the client, and reloads the page. This cookie is then read by PHP and the date_default_timezone_set() function (requires PHP 5 >= 5.1.0) is used to set the default time zone for PHP scripts. Since it runs just after all of the plugins are loaded, it changes the time zone for all of the PHP that follows. It is useful in cases where one wants users to see certain posts on certain days. E.g., if the post slug contains the day number, like “xyz-daynr,” then the post can be specified by “$slug = ‘xyz’.date(j);” in a theme template. The cookie does not expire for ten years, but is reset if the user changes time zones. So, unless the user deletes cookies or moves the page reload only occurs once every ten years.

Copyright 2012-2015 M.D. Green, SaeSolved:: LLC


Bob W Sun 29 Apr 2018 9:28PM

Thanks, Pan. Maybe one can get away with doing this, but the plugin uses the date_default_timezone_set() function which other documentation says that plugins should definitely not do, as it will break other functionality. For the WP core and other plugins to process time as expected, date_default_timezone_set() is supposed to always be set to UTC. So this particular plugin offers a hack that may superficially work, but at the risk of breaking other things.



Bob W Mon 30 Apr 2018 6:25AM

After additional research, my tentative conclusion is (1) I still don't see any plugins that handle per-user time zones correctly, but (2) it looks possible to handle per-user time zones correctly in more recent versions of WordPress. I now expect that by looking at how the broken time zone plugins work, and using what I've learned, it will likely be possible for me to create a plugin that uses an appropriate time zone for the particular user, at least for times displayed by plugins using certain standard display mechanisms.

(My outrage that WordPress can't even handle seasonal time changes is no longer valid, as of more recent releases of WordPress. I do get to be extra scornful of the old WP, though, because they required integer offsets to UTC, which meant they couldn't provide the correct time to the billion people in India, since that would have required a half-integer offset. Anyway, to my delight, it appears that there is a mechanism that allows one to influence the times displayed on a per-user basis, though none of the time zone plugins I've looked at use this mechanism. Instead, they use two different mechanisms, date_default_timezone_set() and update_option('gmt_offset',...), both of which are prone to create date-related bugs.)

Anyway, hot of the trail of solutions... - Bob