PHP + Timezones

Modeps

Lifer
Oct 24, 2000
17,254
44
91
Currently, all the dates and times I have stored in a DB are based off of Chicago Time. I'd like to be able to display these date and times in multiple timezones, dependent upon who is looking at them (a user will input their timezone in their profile, and that should calculate the times and dates that they see on the site.

So basically right now:

- News Post A has a timestamp for Yesterday at 1pm (GMT-5)
- User X comes in and is from Florida (GMT -4) and still sees that News Post A was posted Yesterday at 1pm. Instead I'd like to show him the time as it relates to him.

Is there a quick solution for this? I'd like to keep all the times in the DB consistent and based on server time (all inserts by this user should still be GMT-5), so it would only be the display that is different for the user.

Any suggestions?
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
the code below assumes a couple constants (USER_LOGGEDIN and USER_TIMEZONEOFFSET), which should be self explanatory :)

something like this to just get the local time:
if(USER_LOGGEDIN){
$timezoneoffset = -6;
$timeDiff = ($timezoneoffset * -1);

$timeDiff = (time() + ($timeDiff * 3600));
$timeDiff = ($timeDiff + (USER_TIMEZONEOFFSET * 3600));

$userlocaltime = date("m/d/Y H:i:s", $timeDiff);
}else
$userlocaltime = date("m/d/y H:i:s");

and to convert database dates:
if(USER_LOGGEDIN){
$timezoneoffset = -6;
$timeDiff = ($timezoneoffset * -1);
$datetoconv = $yourRowInWhileLoop["dateTimeFromDb"];
$timeDiff = (strtotime($datetoconv) + ($timeDiff * 3600));
$timeDiff = ($timeDiff + (USER_TIMEZONEOFFSET * 3600));
echo date("M/d/Y g:i A", $timeDiff);
}
else
echo date("M/d/Y g:i A", strtotime($yourRowInWhileLoop["dateTimeFromDb"]));
 

Modeps

Lifer
Oct 24, 2000
17,254
44
91
Makes plenty of sense, but this would mean that I'd have to update every location a date or time appears... :(

Any experience with something like date_default_timezone_set() or date_timezone_set()?
 

Modeps

Lifer
Oct 24, 2000
17,254
44
91
Welp, your fix works very well and is what I'm looking for, I'll just have to make sure I hit up all the locations I write out the time. Thanks!

(ps, threw the first change onto the homepage of my website: http://sochl.com )
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Assuming the user is logged in and you have access to their time offset, why not use the DB function for adjusting the date/time?

I know in MySQL you can use one of a few different functions to do that such as TIMESTAMPADD which would be something like this:

$offset = $user->data['offset']; (or whatever you use to get the user offset info)

$result = $mysqli->query("SELECT TIMESTAMPADD(HOUR, '$offset', date_time) AS date_time FROM table WHERE...");

$row = $result->fetch_assoc();

$date_time = $row['date_time'];

Note: these also work with negative numbers.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
Originally posted by: jjones
Assuming the user is logged in and you have access to their time offset, why not use the DB function for adjusting the date/time?

I know in MySQL you can use one of a few different functions to do that such as TIMESTAMPADD which would be something like this:

$offset = $user->data['offset']; (or whatever you use to get the user offset info)

$result = $mysqli->query("SELECT TIMESTAMPADD(HOUR, '$offset', date_time) AS date_time FROM table WHERE...");

$row = $result->fetch_assoc();

$date_time = $row['date_time'];

Note: these also work with negative numbers.

for ME, mysql resources are more expensive than php resources
not saying that i tested both ways or anything...cuz i didn't (haha)

 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Originally posted by: troytime
Originally posted by: jjones
Assuming the user is logged in and you have access to their time offset, why not use the DB function for adjusting the date/time?

I know in MySQL you can use one of a few different functions to do that such as TIMESTAMPADD which would be something like this:

$offset = $user->data['offset']; (or whatever you use to get the user offset info)

$result = $mysqli->query("SELECT TIMESTAMPADD(HOUR, '$offset', date_time) AS date_time FROM table WHERE...");

$row = $result->fetch_assoc();

$date_time = $row['date_time'];

Note: these also work with negative numbers.

for ME, mysql resources are more expensive than php resources
not saying that i tested both ways or anything...cuz i didn't (haha)
Yeah, I didn't either but for things like this, because I always put as little strain on the DB as possible (using cached pages, cached queries and such), I always try to do these kinds of things using the DB functions if possible. In the long run, it probably makes no difference, I just like using DB functions because I don't get to very often. :D
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
Originally posted by: jjones
Originally posted by: troytime
Originally posted by: jjones
Assuming the user is logged in and you have access to their time offset, why not use the DB function for adjusting the date/time?

I know in MySQL you can use one of a few different functions to do that such as TIMESTAMPADD which would be something like this:

$offset = $user->data['offset']; (or whatever you use to get the user offset info)

$result = $mysqli->query("SELECT TIMESTAMPADD(HOUR, '$offset', date_time) AS date_time FROM table WHERE...");

$row = $result->fetch_assoc();

$date_time = $row['date_time'];

Note: these also work with negative numbers.

for ME, mysql resources are more expensive than php resources
not saying that i tested both ways or anything...cuz i didn't (haha)
Yeah, I didn't either but for things like this, because I always put as little strain on the DB as possible (using cached pages, cached queries and such), I always try to do these kinds of things using the DB functions if possible. In the long run, it probably makes no difference, I just like using DB functions because I don't get to very often. :D

i'm a big fan of db functions too.
i love making big spaghetti queries that take 25 minutes to run :)