You will need to ask where the user is and store it in it's profile in the database. You can't rely on anything actually. As users can be behind a proxy and seem to be like in Shang Hai, but are actually in Melbourne.
For example the user says it is in GMT +1
. You put the option list in the user's profile and when the user saves it, it will be written to a database row called (for example) timezone
.
Try to use the newer DateTime
style (example from user with ID 7);
PHP
$mysqli = new mysqli("localhost", "my_user", "my_password", "database_name");
if ($result = $mysqli->query("SELECT timezone FROM users where user_id = 7")) {
$prefix = '';
if ($result[0]->timezone >= 0) {
$prefix = '+';
}
$date = new DateTime('NOW GMT'.$prefix.$result[0]->timezone);
echo '<pre>';
print_r($date);
echo '</pre>';
}
else {
echo 'Query failed';
}
What happens here is you only save the GMT offset (or use whatever timeset you want) and return the current time based on the user's preference.
Output
DateTime Object (
[date] => 2013-04-01 13:50:08
[timezone_type] => 1
[timezone] => +01:00
)
Don't forget to look into summertime ;)
Good luck!