Page 1 of 1

Adjusting Times per Timezone

Posted: Mon Oct 24, 2011 4:33 pm
by rklockner
I have inheritted an enormous code base that was never written to consider the timezone a user is in. I have been tasked with adding in a timezone field for users to select, then adjust the time everywhere it is used.

There are literally hundreds (if not thousands) of places where the time is displayed.

Hopefully there is a possibility to adjust this without having to adjust every MySQL query.

Ideally,
1 - The query calls the data on the server
2 - Either PHP or MySQL recognizes it as a date and adjusts it based on the user's timezone automatically.

NOTE: I am familiar with how the user can select their timezone in PHP (date_default_timezone_set)

Not Ideal (as this would take weeks to implement),

function scriptToChangeTZ($datetime){
...adjust to new timezone code
}

$datetime = /*date from MySQL query */

$new_datetime = scriptToChangeTZ($datetime);

Re: Adjusting Times per Timezone

Posted: Mon Oct 24, 2011 11:24 pm
by egg82
So let me get this straight: You want users constantly changing your server's timezone settings?
Or do you want to localize the server's timezone to their timezone?

Keep in mind that PHP is a server language, meaning any universal changes are made serverwide, not just clientwide.

Oh, and if it's the second option, this may help:
Image

Just remember your basic math and you should be fine

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 9:08 am
by rklockner
Good point... I do not want them changing the server's timezone. I want to localize it for the user.

Based on your response, it would seem that I have to manually adjust the time each instance a time is called for by the server. Correct?

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 10:02 am
by egg82
yeah, basically (server time +- x) to localize

this should help with the math:

Code: Select all

//x = seconds
//output: mm/dd/yyyy
date("m/d/Y", strtotime(date("m/d/Y"))-x);

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 10:09 am
by Weirdan
date_default_timezone_set() only changes timezone for the current request, so you may use it for datetime localization, like this:

Code: Select all

date_default_timezone_set(isset($_SESSION["userid"]) ? $_SESSION["timezone"] : "UTC");
echo date("Y-m-d H:i:s");

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 10:17 am
by egg82
php.net wrote: date_default_timezone_set() sets the default timezone used by all date/time functions.
Instead of using this function to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone.
that sounds a lot like server time to me

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 10:22 am
by Weirdan
egg82 wrote:that sounds a lot like server time to me
Any setting you change from inside the PHP code (short of rewriting the php.ini with file functions) is not permanent and does not affect other requests - so it's a per-request change.
This includes changes to default timezone - I personally implemented this solution for datetime localization in several projects, including pretty busy sites. It works.

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 10:27 am
by rklockner
The server time is set to Eastern. The scenario I am looking for would be as follows:

$cur_date = "2011-10-05 17:05:33";
$_SESSION["timezone"] = 'America/Chicago';
date_default_timezone_set(isset($_SESSION["timezone"]) ? $_SESSION["timezone"] : "UTC");
echo date("Y-m-d H:i:s", strtotime($cur_date));

Output: 2011-10-05 16:05:33

Your idea works if we are not looking at a time set in a variable, or maybe I am missing something?

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 10:35 am
by egg82
I see what you mean, wierdan. It makes sense, it looks like either I wasn't paying too close attention to the php rules, or php.net worded that a little strangely. I think a little of both.

Well, rklockner,
depends on which idea you want to use. Both will work.
Do you prefer using date_default_timezone_set() or strtotime()?

Since I have little practice with date_default_timezone_set(), I can't help you with that. I think wierdan might, though.

Re: Adjusting Times per Timezone

Posted: Tue Oct 25, 2011 11:26 am
by rklockner
The strtotime() method I would need to alter each time I pulled a new date/time from the database (could take weeks to find all instances), so that is not the ideal scenario.

If I use date_default_timezone_set(), am I able to set the timezone at the beginning of the page and have all occurences of dates automattically adjust like the scenario below?

//NOTE: Server time = 'America/New York'
$cur_date = "2011-10-05 17:05:33";
$_SESSION["timezone"] = 'America/Chicago';
date_default_timezone_set(isset($_SESSION["timezone"]) ? $_SESSION["timezone"] : "UTC");
echo date("Y-m-d H:i:s", strtotime($cur_date));

Desired Output: 2011-10-05 16:05:33

I should also note that the current output is 2011-10-05 17:05:33

Re: Adjusting Times per Timezone

Posted: Wed Oct 26, 2011 6:44 am
by Weirdan
rklockner wrote: If I use date_default_timezone_set(), am I able to set the timezone at the beginning of the page and have all occurrences of dates automatically adjust
Generally yes, but not in your case. Strings are assumed to be in the current time zone (unless another time zone is specified). And what you get from mysql is always string. So doing date("..format..", strtotime($strdate)); does essentially nothing. It would work though if you had your time values in a form of unix timestamps (int) or DateTime objects.

What you could do is to set the time zone on the mysql connection:

Code: Select all

$tz = isset($_SESSION["timezone"]) ? $_SESSION["timezone"] : "UTC";
date_default_timezone_set($tz);
mysql_query("set session time_zone='$tz'");
and then all columns of TIMESTAMP type (but only that type, unfortunately) would be automatically converted back and forth between connection timezone and UTC (timestamp is stored as UTC internally). This is how we have it set up here where I work.

Re: Adjusting Times per Timezone

Posted: Wed Oct 26, 2011 9:03 am
by rklockner
From the responses I am getting, I think I need to:
1 - create a function to adjust the timezone
2 - start implementing the function across the software
3 - gradually implement the new function on all date/time strings
4 - add the ability for user's to change their timezone.

Based on everything I am hearing, this is the way to go. I cannot convert all DATETIMEs to TIMESTAMPs, so this wouldn't be a complete fix.