Javascript 获取用户时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4746249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get user timezone
提问by andrei
Possible Duplicate:
How can I determine a web user's time zone?
可能重复:
如何确定网络用户的时区?
Which is the best way to do this ? php or javascript.Can someone provide me with some snippets ? thanks.
哪种方法最好?php 或 javascript。有人能给我提供一些片段吗?谢谢。
回答by Westy92
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 变量。我使用 jQuery 和 PHP 编写了一个函数。这是经过测试的,确实有效!
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 页面上,将这段代码放在页面顶部附近的某处:
<?php
session_start();
$timezone = $_SESSION['time'];
?>
This will read the session variable "time", which we are now about to create.
这将读取我们现在将要创建的会话变量“time”。
On the same page, in the <head>
section, first of all you need to include jQuery:
在同一页面上的<head>
部分中,首先您需要包含 jQuery:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Also in the <head>
section,, paste this jQuery:
同样在该<head>
部分,粘贴此 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.
您可能注意到也可能没有注意到,但是您需要将 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.phpand point to it with the above url)
最后一件事。您可能想知道 timezone.php 到底是什么。嗯,就是这样:(创建一个名为timezone.php的新文件,并用上面的 url 指向它)
<?php
session_start();
$_SESSION['time'] = $_GET['time'];
?>
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.
如果这工作正常,它将首先加载页面,执行 JavaScript,然后重新加载页面。然后,您将能够读取 $timezone 变量并随意使用它!它返回当前的 UTC/GMT 时区偏移量 (GMT -7) 或您所在的任何时区。
You can read more about this on my blog
您可以在我的博客上阅读有关此内容的更多信息
Cheers
干杯
</Westy92>
</Westy92>
回答by Mikhus
On server-side it will be not as accurate as with JavaScript. Meanwhile, sometimes it is required to solve such task. Just to share the possible solution in this case I write this answer.
在服务器端,它不会像 JavaScript 那样准确。同时,有时需要解决此类任务。只是为了在这种情况下分享可能的解决方案,我写了这个答案。
If you need to determine user's time zone it could be done via Geo-IP services. Some of them providing timezone. For example, this one (http://smart-ip.net/geoip-api) could help:
如果您需要确定用户的时区,可以通过 Geo-IP 服务完成。其中一些提供时区。例如,这个(http://smart-ip.net/geoip-api)可以帮助:
<?php
$ip = $_SERVER['REMOTE_ADDR']; // means we got user's IP address
$json = file_get_contents( 'http://smart-ip.net/geoip-json/' . $ip); // this one service we gonna use to obtain timezone by IP
// maybe it's good to add some checks (if/else you've got an answer and if json could be decoded, etc.)
$ipData = json_decode( $json, true);
if ($ipData['timezone']) {
$tz = new DateTimeZone( $ipData['timezone']);
$now = new DateTime( 'now', $tz); // DateTime object corellated to user's timezone
} else {
// we can't determine a timezone - do something else...
}
回答by Jon Nylander
Just as Oded has answered. You need to have this sort of detection functionality in javascript.
正如奥德所回答的那样。您需要在 javascript 中具有这种检测功能。
I've struggled with this myself and realized that the offset is not enough. It does not give you any information about daylight saving for example. I ended up writing some code to map to zoneinfo database keys.
我自己一直在努力解决这个问题,并意识到偏移量是不够的。例如,它不会为您提供有关夏令时的任何信息。我最终编写了一些代码来映射到 zoneinfo 数据库键。
By checking several dates around a year you can more accurately determine a timezone.
通过检查一年左右的几个日期,您可以更准确地确定时区。
Try the script here: http://jsfiddle.net/pellepim/CsNcf/
试试这里的脚本:http: //jsfiddle.net/pelepim/CsNcf/
Simply change your system timezone and click run to test it. If you are running chrome you need to do each test in a new tab though (and safar needs to be restarted to pick up timezone changes).
只需更改您的系统时区,然后单击运行即可进行测试。如果您正在运行 chrome,您需要在新选项卡中进行每个测试(并且需要重新启动 safar 以获取时区更改)。
If you want more details of the code check out: https://bitbucket.org/pellepim/jstimezonedetect/
如果您想了解代码的更多详细信息,请查看:https: //bitbucket.org/pellepim/jstimezonedetect/