主要网站如何检测时区?
Facebook、myspace、MSN、craigslist;所有这些都可以正确检测时区并显示时间和日期,而无需提交表单或重新加载页面。
他们是如何做到的?我有一个相当大的网站,有人发布需要显示正确时间的文章(根据时区在下午 3 点或 6 点上传)。
预加载器? JavaScript?加载页面并重新加载?我的网站目前让用户进入网站,第一次进入时,它会通过 JavaScript 重新加载页面,并更新其时区信息。然而,这是一件非常烦人的事情,我正在寻找另一种选择。我知道加载后我可以发送 ajax 请求,然后让它使用用户的正确时区更新整个网站的所有内容,但如果我必须单击返回,或重新加载,它会询问我是否要再次发送信息这也很糟糕。那么他们是如何做到的呢?
我正在玩 Facebook,更改我在本地计算机上的位置,它似乎每 5 分钟左右更新一次时区。
我是基于 php、zend 框架的。
Facebook, myspace, MSN, craigslist; all detect timezone and show times and dates correctly without ever submitting a form or reloading a page.
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).
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?
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.
I am php, zend-framework based.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有“时区”请求标头或类似的东西,所以,你不能使用 php,
但使用 javascript 你可以获取客户端的时区偏移量,然后你可以发出 xmlhttp 请求将信息发送到 php
看到这个: http://www.pageloom.com/automatic-timezone-detection-with-javascript漂亮
简单的!
希望这有帮助
there is no "timezone" request header, or something like that, so, you can't with 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
see this: http://www.pageloom.com/automatic-timezone-detection-with-javascript
pretty easy!
hope this helps
您可以使用 IP 地址地理编码找到用户国家/地区,然后拥有一些包含每个国家/地区时区的数组。我想没有其他办法了。
您可以像这样获取用户国家/地区:
然后只需对
$file
使用explode
即可。另请查看这些 Stackoverflow 代码:
如何确定网络用户的时区?
任何可用于确定的可靠 API根据 IP 地址获取用户的城市和国家/地区
You could find user country using IP address geocoding and then have some array with timezones for each country. I think there are no other ways.
You can fetch the user country like this:
Then just use
explode
for the$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
我相信他们使用 IP 寻址和地理位置。这就是全部内容了。
I believe they use IP addressing and Geo-location. That's all there is to it.
首先,您应该在所有级别上使用 UTC,如果您不知道时区,请标记时间。然后在本地客户端上,您可以使用 timezoneOffset
https://developer 调整时间.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
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
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
尝试这个 php 代码,这可以提供更好的精度
Try this php code this gives better precision
jsTimezoneDetect 提供了一个很棒的库,用于通过 JavaScript 自动检测时区。
jsTimezoneDetect provides a great library for automatic timezone detection via javascript.