获取用户时区 [英] get user timezone
问题描述
可能重复:
如何确定网络用户的时区?
这是最好的方法吗? php或javascript.Can有人为我提供了一些片段?谢谢。
Which is the best way to do this ? php or javascript.Can someone provide me with some snippets ? thanks.
推荐答案
这将为您提供作为PHP变量的时区。
我用jQuery和PHP写了一个函数。这已经过测试,并且确实有效!
This will get you the timezone as a PHP variable. I wrote a function using jQuery and PHP. This is tested, and does work!
在PHP页面上,您希望将时区作为变量,请将此片段代码放在靠近顶部的位置page:
On the PHP page where you are want to have the timezone as a variable, have this snippet of code somewhere near the top of the page:
<?php
session_start();
$timezone = $_SESSION['time'];
?>
这将读取会话变量time,我们现在要创建它。
This will read the session variable "time", which we are now about to create.
在同一页面的< head>
部分,首先需要包含jQuery:
On the same page, in the <head>
section, first of all you need to include jQuery:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
同样位于< head>
部分,,粘贴此jQuery:
Also in the <head>
section,, paste this jQuery:
<script type="text/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://domain.com/timezone.php",
data: 'time='+ visitortimezone,
success: function(){
location.reload();
}
});
}
});
</script>
您可能已经注意到或未注意到,但您需要将网址更改为您的实际域名。
You may or may not have noticed, but you need to change the url to your actual domain.
最后一件事。你可能想知道什么是timezone.php。嗯,就是这样:
(创建一个名为 timezone.php 的新文件并用上面的url指向它)
One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this: (create a new file called timezone.php and point to it with the above url)
<?php
session_start();
$_SESSION['time'] = $_GET['time'];
?>
如果这项工作正常,它将首先加载页面,执行JavaScript并重新加载页面。然后,您将能够阅读$ timezone变量并将其用于您的乐趣!它返回当前的UTC / GMT时区偏移量(GMT -7)或您所在的任何时区。
If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.
您可以在我的博客
干杯
< / Westy92>
这篇关于获取用户时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!