Auto-detecting user's timezone

  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8212
  • Loc: USA
  • Status: Offline

Post May 6th, 2009, 3:16 pm

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 :)
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 6th, 2009, 3:16 pm

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post May 6th, 2009, 3:58 pm

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.
I'd love to change the world, but they won't give me the source code.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8212
  • Loc: USA
  • Status: Offline

Post May 6th, 2009, 4:10 pm

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?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post May 6th, 2009, 4:34 pm

I would think the offset would be better to hang on to, but I guess that won't really account for DST...
I'd love to change the world, but they won't give me the source code.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8212
  • Loc: USA
  • Status: Offline

Post May 6th, 2009, 5:12 pm

So, how do I go about changing the user's time based on their timezone? time() + $offset; ?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • lullabee
  • Born
  • Born
  • No Avatar
  • Joined: Mar 18, 2010
  • Posts: 1
  • Status: Offline

Post March 18th, 2010, 11:09 pm

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
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post March 18th, 2010, 11:44 pm

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.
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8212
  • Loc: USA
  • Status: Offline

Post March 19th, 2010, 8:06 pm

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).
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • raz1now
  • Born
  • Born
  • No Avatar
  • Joined: May 11, 2010
  • Posts: 1
  • Status: Offline

Post May 12th, 2010, 12:18 am

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 204 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.