• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PHP + Timezones

Modeps

Lifer
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?
 
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"]));
 
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()?
 
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 )
 
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.
 
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)

 
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. 😀
 
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. 😀

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


 
Back
Top