TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Users Timezone (http://www.talkphp.com/general/1232-users-timezone.html)

CMellor 09-26-2007 12:22 AM

Users Timezone
 
Hey, hope all are well. I have a question.

My project is pretty big, and can only hope for global success. Currently all the times on the website are using the server's clock, which is 6 hours behind my clock (It's in the USA and I'm in the UK). I will allow my users to set there timezone via a select menu, the same as a lot of forums (including this one) use in it's control panel. I'll be storing the value in the 'member' table.

What I'm not 100% sure on is how to use the value to display the time in their timezone. My timezone would be '0 - GMT' but with the server time been six hours behind my clock, would I add '+6' instead? When I output a time, I want it to be the time in their timezone, so, my first though was to do something like this:

Code:

echo date('D M, Y' time()+$row->timezone);
If anyone has any discussions on this, I look forward to hearing them :)

P.S. Hope I made sense :P

Wildhoney 09-26-2007 09:14 AM

I'd go one of two, maybe three ways at a push, in achieving this.

Method 1: Using strtotime()
Downsides: Not the fastest function in the world but gets the job done.

PHP Code:

echo date('H:i:s D M, Y'strtotime('+6 hours')); 

Method 2: Using mktime()
Downsides: Long line. Uses functions inside functions which can look exceptionally messy.

PHP Code:

echo date('H:i:s D M, Y'mktime(date('H') + 6date('m'), date('Y'), date('m'), date('d'), date('y'))); 

Method 3: The proper way! Using date_default_timezone_set or putenv
Downsides: Very little! Just wish this function was more heard of.

PHP Code:

putenv('TZ=US/Eastern'); // Using putenv. Good for pre PHP5.
echo date('H:i:s D M, Y'); 

PHP Code:

date_default_timezone_set('US/Eastern'); // How to do it now we have PHP5
echo date('H:i:s D M, Y'); 


CMellor 09-26-2007 10:12 PM

While date_default_timezone_set looks tempting, I can't see how it would work with the values of the select menu, which will be '0, +1, +2... -2, -1' etc. Unless those values would work with the $timezone_identifier, but I see no mention of that in the manual. I think I may just go with strtotime()

Unless anyone has any follow up's to my query, thanks for the help Wild Honey.


All times are GMT. The time now is 03:55 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0