Timezones

Rjs37

Member
Well I'm continuing my integration of xenforo with my own application and the next thing I was looking into was timezones and using the user offsets to show times specific to a user's setup.

One thing that really confused me is that Xenforo (for a few hours I thought it was my PHP Library - Cakephp) was changing the timezone to UTC (whereas I had it set to Europe/London.

An apparent side effect of that is below (the latter line being the correct time):

PHP:
echo "Time: ".time()." Date: ".date("D d M Y, g:i A T", time())."<br />";

With Xenforo
Time: 1309039177 Date: Sat 25 Jun 2011, 9:59 PM UTC

Without Xenforo
Time: 1309039177 Date: Sat 25 Jun 2011, 10:59 PM BST

If I changed my code to use Xenforo's own timezone/locale function like so:

PHP:
echo "Time: ".time()." Date: ".date("D d M Y, g:i A T", time()+(Xenforo_Locale::getTimeZoneOffset()))."<br />";

It instead produced this:
Time: 1309039177 Date: Sat 25 Jun 2011, 10:59 PM UTC

Which produces the correct time but the wrong timezone information. That time is BST or UTC+1 not UTC. If I displayed that to the end user then it would be giving the wrong information.

If I'm approaching this wrong then I'd appreciate it if someone could point me in the right direction.
 
You can use Xenforo_Locale::dateTime(time(), "D d M Y, g:i A T");

You're loading the offset which is just a number and has no concept of timezone. Another way is to use Xenforo_Locale::getDefaultTimeZone() and see if there is a timezone and set that on the Date Object.
 
It doesn't look like dateTime accept's a date format like that. That prints out: Jun 28, 2011 at 1:32 AM instead.

My main problem was that in Xenforo's code it was changing how the default PHP date function behaved (by setting the timezone to UTC in it's initialization). And when it's set to that it (the normal date function) doesn't seem to be able to recognize that it's in BST (instead thinks it is UTC). So possibly that's an error in the code-base (that it sets it to UTC and not Europe/London).

I've got around that by setting the timezone myself straight after the Xenforo initialization using the timezone value from the logged in user. That uses Europe/London which then works fine with the default php date function when printing out the timezone.
 
Top Bottom