Auto-detecting user's timezone

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Is it possible to get the user's timezone automatically with PHP?

Also, where could I get all possible time-zones listed on one page? (Not categorized by nation/country as it is on PHP.NET).

Thanks :)
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

That's usually done with javascript, something like:

Code: [ Select ]
 
var timeZone = (new Date().gettimezoneOffset()/60)*(-1);
 
  1.  
  2. var timeZone = (new Date().gettimezoneOffset()/60)*(-1);
  3.  


But I did dig up an interesting article with some nice alternatives (PHP included). I've read about other services like IP-to-lat/lon databases & IP locator services, but JavaScript was sooo much less of a hassle that I always stuck with that.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

The reason I need this is so I can store that timezone to my database... how am I going to do that in JavaScript?

Right now, the part that does that is:
PHP Code: [ Select ]
<?php
$sql = array(
    'UID' => null,
    'username' => $_POST['username'],
    'password' => md5($pass),
    'email' => $_POST['email'],
    'name' => $_POST['name'],
    'avatar' => '',
    'birthdate' => $byear . '-' . $bmonth . '-' . $bday,
    'reg_date' => time(),
    'activate_id' => rand(0, 99999999) . "/{$_POST['username']}",
    'active' => 0,
    'permission' => 3,
    'permissions' => $auth->def_perms,
    'style' => 'wedevoy',
    'website' => isset($_POST['website']) ? $_POST['website'] : null,
    'secret_question' => $_POST['secret_question'],
    'secret_question_ans' => $_POST['answer_secret_question'],
    'allow_email' => 1,
    'show_email' => 0,
    'contact_type' => 1,
    'signature' => null,
    'timezone' => '<span style="font-weight: bold">GMT</span>',
    'costume_title' => null,
    'user_warnings' => 0,
    'ip' => ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']),
    'forum_posts' => 0,
    'comment_posts' => 0,
    'current_page' => null,
    'profile_views' => 0,
    'site_visits' => 0,
    'site_rank' => 'Regular User',
    'last_visit' => null,
    'MSN' => null,
    'tutorials' => '0'
);
?>
  1. <?php
  2. $sql = array(
  3.     'UID' => null,
  4.     'username' => $_POST['username'],
  5.     'password' => md5($pass),
  6.     'email' => $_POST['email'],
  7.     'name' => $_POST['name'],
  8.     'avatar' => '',
  9.     'birthdate' => $byear . '-' . $bmonth . '-' . $bday,
  10.     'reg_date' => time(),
  11.     'activate_id' => rand(0, 99999999) . "/{$_POST['username']}",
  12.     'active' => 0,
  13.     'permission' => 3,
  14.     'permissions' => $auth->def_perms,
  15.     'style' => 'wedevoy',
  16.     'website' => isset($_POST['website']) ? $_POST['website'] : null,
  17.     'secret_question' => $_POST['secret_question'],
  18.     'secret_question_ans' => $_POST['answer_secret_question'],
  19.     'allow_email' => 1,
  20.     'show_email' => 0,
  21.     'contact_type' => 1,
  22.     'signature' => null,
  23.     'timezone' => '<span style="font-weight: bold">GMT</span>',
  24.     'costume_title' => null,
  25.     'user_warnings' => 0,
  26.     'ip' => ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']),
  27.     'forum_posts' => 0,
  28.     'comment_posts' => 0,
  29.     'current_page' => null,
  30.     'profile_views' => 0,
  31.     'site_visits' => 0,
  32.     'site_rank' => 'Regular User',
  33.     'last_visit' => null,
  34.     'MSN' => null,
  35.     'tutorials' => '0'
  36. );
  37. ?>


I bolded what I'm talking about. (Or should I use offset rather then timezone?)

Also, the following test:
Code: [ Select ]
<script type="text/javascript">
var x = new Date()
var timeZone = currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60


document.write('Timezone is: ' + timeZone);
</script>
  1. <script type="text/javascript">
  2. var x = new Date()
  3. var timeZone = currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60
  4. document.write('Timezone is: ' + timeZone);
  5. </script>
gives me:

Timezone is: 5

... That's offset, correct? +5 from GMT?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I would think the offset would be better to hang on to, but I guess that won't really account for DST...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

So, how do I go about changing the user's time based on their timezone? time() + $offset; ?
  • lullabee
  • Born
  • Born
  • lullabee
  • Posts: 1

Post 3+ Months Ago

You can determine the user’s timezone data by checking the user’s IP address against a GeoLocation database like from IP2Location . You can also grab all the timezones from the same database.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Quote:
Is it possible to get the user's timezone automatically with PHP?


Not as reliably as you would be able to with Javascript and a <form> field in front of the user.

Based on the details I see in the example code and those details generally being something you're asking a visitor for with a <form> anyways, you should ask the user for their timezone in a form field and use Javascript to preset the value of that field with a best guess for their timezone.

I would stay away from using a GeoIP database unless there is absolutely no chance of getting the user to turn over this information and whatever the information is being used for is an optional variable. Realistically, serving advertisements is probably going to be the only thing this would work for.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Alright then... I must not have a great need for this because I didn't really press anyone here for an answer to my question, but I got an idea (that someone probably already implemented and created a class for) that would implement the timezone unrealistically (Not for ads :lol: ).

How do I use the offset derived from the JavaScript (Or whatever that number is... see my last two posts).
  • raz1now
  • Born
  • Born
  • raz1now
  • Posts: 1

Post 3+ Months Ago

hi,
that's a solution i made last night

Code: [ Select ]
<?php
if (!isset($_POST["timezoneoffset"])){
?>
 <form method="post" action="<?php echo $_SERVER["PHP_SELF"];  ?>" id="time_form" name="time_form">
 <script type="text/javascript">
  tzo = - new Date().getTimezoneOffset()*60;
  document.write('<input type="hidden" value="'+tzo+'" name="timezoneoffset">');
 </script>
 <input type="submit" value="Get Server Client TimeZone Difference" name="Ok">
 </form>
<?php
}else{
 $serverTimezoneOffset = (date("O") / 100 * 60 * 60);
 echo 'Server Timezone Offset : ' . ($serverTimezoneOffset/(60*60)) .' hours';
 echo '<br />';
 $clientTimezoneOffset = $_POST["timezoneoffset"];
 echo 'Client Timezone Offset : ' . ($clientTimezoneOffset/(60*60)) .' hours';
 echo '<br />';
 $serverTime = time();
 echo 'Server date time : ' . strftime("%d %b %Y %H:%M", $serverTime);
 echo '<br />';
 $serverClientTimeDifference = $clientTimezoneOffset-$serverTimezoneOffset;
 $clientTime = $serverTime+$serverClientTimeDifference;
 echo 'Client date time : ' . strftime("%d %b %Y %H:%M", $clientTime);
 echo '<br />';
 echo 'Difference : ' . ($serverClientTimeDifference/(60*60)) . " hours";
 echo '<br />';
}
?>
  1. <?php
  2. if (!isset($_POST["timezoneoffset"])){
  3. ?>
  4.  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];  ?>" id="time_form" name="time_form">
  5.  <script type="text/javascript">
  6.   tzo = - new Date().getTimezoneOffset()*60;
  7.   document.write('<input type="hidden" value="'+tzo+'" name="timezoneoffset">');
  8.  </script>
  9.  <input type="submit" value="Get Server Client TimeZone Difference" name="Ok">
  10.  </form>
  11. <?php
  12. }else{
  13.  $serverTimezoneOffset = (date("O") / 100 * 60 * 60);
  14.  echo 'Server Timezone Offset : ' . ($serverTimezoneOffset/(60*60)) .' hours';
  15.  echo '<br />';
  16.  $clientTimezoneOffset = $_POST["timezoneoffset"];
  17.  echo 'Client Timezone Offset : ' . ($clientTimezoneOffset/(60*60)) .' hours';
  18.  echo '<br />';
  19.  $serverTime = time();
  20.  echo 'Server date time : ' . strftime("%d %b %Y %H:%M", $serverTime);
  21.  echo '<br />';
  22.  $serverClientTimeDifference = $clientTimezoneOffset-$serverTimezoneOffset;
  23.  $clientTime = $serverTime+$serverClientTimeDifference;
  24.  echo 'Client date time : ' . strftime("%d %b %Y %H:%M", $clientTime);
  25.  echo '<br />';
  26.  echo 'Difference : ' . ($serverClientTimeDifference/(60*60)) . " hours";
  27.  echo '<br />';
  28. }
  29. ?>


you can use the hidden input script on a login form
after submit you can store the $serverClientTimeDifference in a $_SESSION variable
and use it whenever you want ;)

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 84 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.