How are major sites detecting timezone? [duplicate

2019-03-09 15:34发布

This question already has an answer here:

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.

6条回答
啃猪蹄的小仙女
2楼-- · 2019-03-09 15:51

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

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-09 15:57

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:

<?php
$file = file_get_contents("http://api.hostip.info/get_html.php?ip=".$_SERVER['REMOTE_ADDR']."");
?>

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

查看更多
干净又极端
4楼-- · 2019-03-09 16:03

jsTimezoneDetect provides a great library for automatic timezone detection via javascript.

查看更多
混吃等死
5楼-- · 2019-03-09 16:05

I believe they use IP addressing and Geo-location. That's all there is to it.

查看更多
Explosion°爆炸
6楼-- · 2019-03-09 16:09

Try this php code this gives better precision

  <?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];?>
查看更多
萌系小妹纸
7楼-- · 2019-03-09 16:18

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

查看更多
登录 后发表回答