
Get User Timezone
Question:
How can I determine a web user's timezone using PHP or JavaScript?
Answer:
PHP Function:
<code class="php">session_start(); $timezone = $_SESSION['time'];</code>
JavaScript Function:
<code class="javascript">$(document).ready(function() {
if ("<?php echo $timezone; ?>".length == 0) {
var visitortime = new Date();
var visitortimezone = "GMT " + -visitortime.getTimezoneOffset() / 60;
$.ajax({
type: "GET",
url: "http://example.com/timezone.php",
data: 'time=' + visitortimezone,
success: function() {
location.reload();
}
});
}
});</code>PHP Server-Side Script (timezone.php):
<code class="php">session_start(); $_SESSION['time'] = $_GET['time'];</code>
Process:
By executing this process, you can determine the user's timezone and store it in the PHP session variable $timezone.
The above is the detailed content of How to Determine a User\'s Timezone Using PHP and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!