Forum Moderators: coopster

Message Too Old, No Replies

Time Zone

php4

         

benlieb

4:47 pm on May 25, 2006 (gmt 0)

10+ Year Member



What's the best way to deal with the difference in server time vs. client time? I'm running php4 not 5.

jatar_k

4:49 pm on May 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you have logged in users then you could have them store their offset in the db and use that to display their local time. That is how most forums do it.

if all of your users are in the same area then just use that offset as your standard display time.

benlieb

1:00 am on May 26, 2006 (gmt 0)

10+ Year Member



Thanks for the advice.

All the users are in one place but the server is in another.

What you say make sense logically, but how do I implement this offset? Do I add a certain number of seconds to the timestamp and then use time() or date()?

jatar_k

3:13 am on May 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try this one
[php.net...]

benlieb

5:19 am on May 26, 2006 (gmt 0)

10+ Year Member



I already have the time stamp from:

$now = time();

but that gives me the time of the server.

So the question is, is the following the most effictient way to handle the time difference between user and server:

//difference in hours between users and server
$hours_diff = 5;
$secs_diff = $hours_diff * 60 * 60;
$server_time = time();
$user_time = $server_time + $secs_diff;
echo "the time is: " . date("F j, Y, g:i a", $user_time);

This seems a little gerry-rigged to me, and I can see potential problems. For example in my case I am actually recording the time that certain things were done in a database, like when an appointment was added. Do I store the server timestamp or the modified server timestamp plus the offset? What I mean is do I convert the time when displaying the time or when storing the time? So two years from now if another programmer looks at my code, how will he know that the stored timestamp is the orignial server timestamp or the modified user timestamp? If I store the modified stamp and he/she thinks it's the original it could be compensated twice for the offset. And as data gets manipulated often, this could happen several times for one piece of data. It seems messy.

If I'm making no sense, ignore me.

benlieb

6:05 am on May 30, 2006 (gmt 0)

10+ Year Member



ok, ok, don't ignore me...

jatar_k

4:21 pm on May 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry benlieb, I honestly thought I had answered after your last post

I would store the server timestamp, then convert it on display

using strtotime is a little easier than what you have. Consider we have the var $server_time that could come from the db or from a call to time()

$user_time = strtotime("+5 hours", $server_time);
echo "the time is: " . date("F j, Y, g:i a", $user_time);

should work

benlieb

4:33 pm on May 30, 2006 (gmt 0)

10+ Year Member



jatar_k:

Thanks for the advice and the strtotime tip. I had never thought to use it that way. It makes life a little easier.