Click to See Complete Forum and Search --> : Timezone madness! Help appreciated


Teach
08-04-2008, 08:54 AM
Hi guys. I hope you can help me with these please... I'm completely stumped

We use a timezone selection for locale, and I want to further use that to basically (when someone is logged in) to show all the website dates in their own time. This includes dates stored in our MySQL database.

The timezone selections are at http://pastebin.com/mf714dfa (too large to post here), these are the values that are in our `timezone` field in our `user` table

So, that's fine...

In my functions.php, included by all files I've tried the following:


$search=mysql_fetch_array(mysql_query("SELECT * FROM user WHERE account='".mysql_real_escape_string($_SESSION['account'])."'"));
$timezone=$search['timezone'];
putenv("TZ=$timezone");
mktime(0,0,0,1,1,1970);


and also I tried:


date_default_timezone_set($timezone);


So the problems:

- For some dates, it seems to work fine. But on all our datetimes (stored as yyyy-mm-dd hh:mm:ss) it doesn't change them. They remain in the London timezone where the server is hosted

- Secondly, when inserting stuff into the database, we use:
$dateadded=date('Y-m-d H:i:s');
With the new timezone, it inserts it under their time (which is expected) but we don't want that to happen. All the times in the SQL database should be local to the server, and then output differently...

For the moment I've disabled all this to save it screwing up our database.

Any help is much appreciated. Thank you

Phill Pafford
08-04-2008, 09:39 AM
I would suggest using JavaScript to get the user's timezone and pass that for any time calculations you want to do.

http://devblog.redfin.com/2007/08/getting_the_time_zone_from_a_web_browser.html

http://www.webxpertz.net/forums/showthread.php?t=22857

Teach
08-04-2008, 09:47 AM
Hi Phill, thanks for your reply

I don't think JAvascript could do this as good as PHP can (when it works!) hehe

I've managed to sort the issue out:

---
- Secondly, when inserting stuff into the database, we use:
PHP Code:
$dateadded=date('Y-m-d H:i:s');
With the new timezone, it inserts it under their time (which is expected) but we don't want that to happen. All the times in the SQL database should be local to the server, and then output differently...
---

Sorted this with using gmdate();

It's just strange. normal date()'s echo the correct time for the timezone, but when selecting the date from the database it doesn't. I thought as it strtotime()'s the date first, it would pop it into the user's timezone :(

Confusing!

Teach
08-04-2008, 12:08 PM
Just to add to this, it's not just strtotime()'ing dates from my database

If I try a date("l dS M Y H:i", strtotime('2004-08-12 10:18')); it still has the same error. Weird!