javascript 主要网站如何检测时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7696215/
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
How are major sites detecting timezone?
提问by Darius
Facebook, myspace, MSN, craigslist; all detect timezone and show times and dates correctly without ever submitting a form or reloading a page.
Facebook、myspace、MSN、craigslist;所有都检测时区并正确显示时间和日期,而无需提交表单或重新加载页面。
How do they do it? I have a pretty large site and have people publishing articles which need to display correct times (uploaded 3pm or 6pm depending on timezone).
他们是如何做到的呢?我有一个非常大的网站,有人发布需要显示正确时间的文章(根据时区在下午 3 点或下午 6 点上传)。
Preloaders? Javascript? Loading the page and reloading it? My site currently has the person enter the site and upon first entry it reloads the page with updated information of their timezone through javascript. This is however a pretty annoying thing and am looking for another alternative. I know that upon loading I can send out an ajax request and then have it update everything throughout the site with the correct timezone for the user BUT if I have to click back, or reload it asks if I'd like to send the information again which is also bad. So how do they do it?
预加载器?Javascript?加载页面并重新加载它?我的网站目前让该人进入该网站,并在第一次进入时通过 javascript 重新加载包含其时区更新信息的页面。然而,这是一件非常烦人的事情,我正在寻找另一种选择。我知道在加载时我可以发送一个 ajax 请求,然后让它用用户的正确时区更新整个站点的所有内容,但是如果我必须点击返回,或者重新加载它会询问我是否想再次发送信息这也不好。那么他们是如何做到的呢?
I was playing around with Facebook, changing my location on my local machine and it seemed to update the timezone every 5 minutes or so.
我正在玩 Facebook,在我的本地机器上更改我的位置,它似乎每 5 分钟左右更新一次时区。
I am php, zend-framework based.
我是 php,基于 zend 框架。
回答by
there is no "timezone" request header, or something like that, so, you can't with php
没有“时区”请求标头,或者类似的东西,所以,你不能用 php
but with javascript you can get the timezone offset of the client, then you could make a xmlhttp request to send informations to php
但是使用 javascript 您可以获得客户端的时区偏移量,然后您可以发出 xmlhttp 请求将信息发送到 php
see this: http://www.pageloom.com/automatic-timezone-detection-with-javascript
看到这个:http: //www.pageloom.com/automatic-timezone-detection-with-javascript
pretty easy!
相当容易!
hope this helps
希望这可以帮助
回答by Olli
You could find user country using IP address geocodingand then have some array with timezones for each country. I think there are no other ways. You can fetch the user country like this:
您可以使用 IP 地址地理编码找到用户国家/地区,然后使用每个国家/地区的时区数组。我认为没有其他方法。您可以像这样获取用户国家/地区:
<?php
$file = file_get_contents("http://api.hostip.info/get_html.php?ip=".$_SERVER['REMOTE_ADDR']."");
?>
Then just use explode
for the $file
.
然后只explode
用于$file
.
Look also these Stackoverflow codes:
How can I determine a web user's time zone?
Any Reliable API available to determine User's city and country from IP address
另请查看这些 Stackoverflow 代码:
如何确定网络用户的时区?
任何可用于从 IP 地址确定用户所在城市和国家的可靠 API
回答by Mob
I believe they use IP addressing and Geo-location. That's all there is to it.
我相信他们使用 IP 地址和地理位置。这里的所有都是它的。
回答by Adrian World
First off, you should work with UTC across all levels and if you don't know a time zone mark the time as such. Then on local client you can adjust the time with the timezoneOffset
首先,您应该在所有级别上使用 UTC,如果您不知道时区,则将时间标记为这样。然后在本地客户端上,您可以使用 timezoneOffset 调整时间
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
回答by pckabeer
Try this php code this gives better precision
试试这个 php 代码,它可以提供更好的精度
<?php $ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");
$json = json_decode($json,true);
$timezone = $json[localTimeZone];?>
回答by Jordan Magnuson
jsTimezoneDetectprovides a great library for automatic timezone detection via javascript.
jsTimezoneDetect提供了一个很棒的库,用于通过 javascript 自动检测时区。