Topic: Time zone depending on PC system time

Is there a way (mod) to make PunBB set the time zone automatically for users depending on their PC system time?

Some of my users requested such a thing since they often move over different places with different timezones. They remember to change their system time on PC but not always remember to change timezone in their punbb profile.
That would be helpful.

Re: Time zone depending on PC system time

Anyone?

Re: Time zone depending on PC system time

Download this file and upload it into your punbb directory on your server

http://www.ic-gallery.com/stuff/tz.js

Then modify your main.tpl to include the file - add this in between <head> and </head>

<script type="text/javascript" src="tz.js"></script>

Then save this code as timezone.php and upload that into your punbb directory on your server

<?php

define('PUN_ROOT', './');
require PUN_ROOT.'include/common.php';

$timezone = $_GET['timezone'];

if (!is_numeric($timezone)) die('Invalid timezone');
if ($pun_user['id'] < 2) die('Not logged in');

$db->query("UPDATE {$db->prefix}users SET timezone = '$timezone' WHERE id = '{$pun_user['id']}' LIMIT 1");

echo $db->affected_rows();

That will check the timezone of visitors once per session and update the timezone in their profile where required. You can of course comment out or remove the alert() if you wish.

Re: Time zone depending on PC system time

Thanks a lot!!! Seems to work properly. We'll see feedback from my users.

Re: Time zone depending on PC system time

Can't ask for better support than that smile

"Programming is like sex: one mistake and you have to support it for the rest of your life."

6 (edited by pabbito 2005-12-04 20:48)

Re: Time zone depending on PC system time

Seems to work properly

I said it to early. Seems to work but incorrect.

There is something strange.

My timezone is CET. I set this in admin panel and in my profile. After puting the mod you suggested it adjusts my timezone to -01. That makes my entries' time 2h earlier? Why? Am I doing someting wrong? Can you or anyone check thw script somehow?

Re: Time zone depending on PC system time

The board timezone is supposed to be set so that the other timezones are correct (so, if the default time on the server is CET, I choose the CET timezone, so the forum time is GMT). Then, each individual user has their own setting

8 (edited by pabbito 2005-12-04 22:34)

Re: Time zone depending on PC system time

So if server timezone is CET,  should  I set my personal timezone in my profile on GMT ??? Am I getting it right?
But it can't be correct, because CET is +01, GMT is 00 and the script substracts one hour. So my entry gets time 2 hours less than it should.

Can anyone explain to me, how to choose the correct timezone, and why the script adjusts it to -1 ??

I checked the tomezone on server with date("T"). It's CET for sure.

Re: Time zone depending on PC system time

If the timezone on the server (without any of the PunBB stuff interfering) is CET, then PunBB's server timezone should be set as CET. That means the forum clock, by itself, is GMT. Then, you pick the appropriate offset on a per-user level. So you'd pick CET for yourself as well.

Re: Time zone depending on PC system time

OK, that is understandable Smartys. And it works.

But, WHY the script, Nibbler(cpg) suggested, makes it always -1. It should stay unchanged and is supposed to make adjustment only when it is needed.

Re: Time zone depending on PC system time

That's exactly what it does do. When the user arrives at the site the javascript determines the timezone offset of the client and sends it to the php script that then updates the timezone serverside. If the timezone was actually changed then you get the message. If the timezone was already correctly set then you get no message. Either way, a cookie is then set so that no further checks occur during that session. If you can post a link and a test user account then I can see what is happening.

Re: Time zone depending on PC system time

Nibbler(cpg), I don't know how it happens, but still it always sets my timezone to -1.

What I do is:
1) I set my timezone to CET
2) Logout
3) I delete the cookie resposible for timezone
4) Login, and the script alert me that it set the timezone to -1

My PC timezone is also CET (GMT+1). System - WinXP

I made an experiment. I changed the timezone on my PC for GMT-6. Then deleted cookies, then ran my forum, and it set timezone to -1 again. So it doesn't matter what the timezone is, it always tries to make it -1

Re: Time zone depending on PC system time

If your browser doesn't tell the script the correct timezone then there's nothing I can do about it. Maybe it's a browser issue, I can't test on IE or on mac browsers.

Re: Time zone depending on PC system time

Installed this on my punbb. It adjusted my profile timezone to GMT +3.5, when the correct TZ is GMT -3.5. Tested using Firefox 1.5 on Windows (2000). Yes, my system TZ is set correctly smile

Re: Time zone depending on PC system time

Nibbler(cpg) wrote:

If your browser doesn't tell the script the correct timezone then there's nothing I can do about it. Maybe it's a browser issue, I can't test on IE or on mac browsers.

I'm using Firefox 1.5

Re: Time zone depending on PC system time

Stick a - in then

var offseth = -offsetmin/60

I live in GMT as you may have guessed tongue

Re: Time zone depending on PC system time

Nibbler(cpg) wrote:

Stick a - in then

var offseth = -offsetmin/60

I live in GMT as you may have guessed tongue

Surely thats going to break + timezones? tongue

Re: Time zone depending on PC system time

Now much better smile
Thanks a lot !!

19 (edited by sleddog 2005-12-06 21:24)

Re: Time zone depending on PC system time

I can confirm that it now works correctly for both - and + timezones (I changed my system TZ to check). Also, it works with IE6 as well as Firefox 1.5 (on Windows 2000).

Very nice work Nibbler, thanks.

ps. Maybe you should update the linked js file (in your first post)?

Re: Time zone depending on PC system time

I was wondering, what if you included the js in the html file, made php give javascript the current timezone and javascript only do an update if its different, and lose the cookie stuff completely, would that make it slightly nicer?

Re: Time zone depending on PC system time

It would make one more query everytime the script is run. Not everyone likes it.

Re: Time zone depending on PC system time

No, it would make 1 extra javascript "if" it would only run a query if the timezone needed changing.

Re: Time zone depending on PC system time

True. I forgot that the timezone is taken form db anyhow to make the time correct.

Re: Time zone depending on PC system time

I find myself in somewhat the same situation, only with different circumstances. The box is located in UTC-5 (EST) and I'm located in UTC+1 along with everyone else who uses the forum. I did the decent thing and filled "Server time zone" with "-05 EST", and when I configure my own account to be "+01 CET", it displays nicely. Only my problem is then: it only displays nicely if you have an account with a correctly configured time zone. Most of the forum visitors are anonymous including when they post, and these are neglected because there isn't another box in the "Options" menu that says "Number of time zones with which to adjust the server time". The thing is, if I simply cheat and put the wrong time zone under the first option, anybody who have rightfully filled in +01 CET will get wrong times because of the assumption that the server's time zone relative to the user's time zone is actually real.

Has anybody done anything like this before? It's only adding a number to the time if a person isn't logged in, really.

Re: Time zone depending on PC system time

Try hardcoding it in here:

$pun_user['timezone'] = $pun_config['o_server_timezone'];
$pun_user['timezone'] = +1;

functions.php