Client-side timezone handling problems

I use the function below to set the timezone in PHP based on the offset returned by the client browser (thanks to jason at jasonpriem dot com

But, my database queries don't seem to return different date displays... do I need to tell mySQL separately from PHP? I thought if I told PHP, PHP make sure the DB also operates in the right timezone. E.g., a query that records 1pm MST (my timezone), is displayed as 1pm no matter what I set the default timezone to.
function set_tz_by_offset($offset) {
        $offset = $offset*60*60;
        $abbrarray = timezone_abbreviations_list();
        foreach ($abbrarray as $abbr) {
            //echo $abbr."<br>";
                foreach ($abbr as $city) {
                    //echo $city['offset']." $offset<br>";
                        if ($city['offset'] == $offset) { // remember to multiply $offset by -1 if you're getting it from js
                               date_default_timezone_set($city['timezone_id']);
                               return true;
                        }
                }
        }
    date_default_timezone_set("ust");
       return false;
       }

Open in new window

metalaureateAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nasirbestCommented:
I am not aware with your database query. but I think following solution can help you.

1. while saving in database never use regular dates instead use unix timestamp, and don't worry about timezones because unix timestamp does not depends on zone setting.

2. and when you have to display date back to user, get timestamp from db, add offset, and then convert timestamp into regular date.


here is sample code
<?php

// save some date
mysql_query('INSERT INTO tbl_my (column1) VALUES (UNIX_TIMESTAMP())');


// display date back to user
$test_time = mysql_result(mysql_query('SELECT column1 FROM tbl_my LIMIT 1'));
$test_time += $offset;  // add timezone difference (between server and client) in timestamp

echo date('Y-m-d H:i:s', $test_time);

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
metalaureateAuthor Commented:
I wouldn't mind some other experts weighing on this as it's rather critical to a lot of code...
0
metalaureateAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.