php 如何自动检测用户的时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5203382/
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 to automatically detect user's timezone?
提问by Balaji Chandrasekaran
hi i am creating a web application , where if a user register we will show the created date.
嗨,我正在创建一个 Web 应用程序,如果用户注册,我们将显示创建日期。
For that we are using Current time stamp in my sql table.Which shows server Time.But we don't know how to convert Time according to user time zone.
为此,我们在我的 sql 表中使用当前时间戳。显示服务器时间。但我们不知道如何根据用户时区转换时间。
Because we are not getting user's country.
因为我们没有得到用户的国家。
Can any on help me to fix it
任何人都可以帮我修复它吗
Thanks in advance :)
提前致谢 :)
回答by supersuphot
Use javascript solution
使用javascript解决方案
http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/
http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/
Demoshould show your timezone in select box.
演示应该在选择框中显示您的时区。
http://onlineaspect.com/examples/timezone/index.html(Dead link)
回答by cusimar9
You can't get a user's timezone on the server side, and you can only make a guess at it on the client side.
在服务器端无法获取用户的时区,只能在客户端进行猜测。
If you rely on getting the user's IP address then you could geolocate that and deduce a time.
如果您依赖于获取用户的 IP 地址,那么您可以对其进行地理定位并推断出时间。
The way this is usually done is by asking the user (when they register, for instance) what timezone they are in and then use this in your time calculations.
通常这样做的方法是询问用户(例如,当他们注册时)他们所在的时区,然后在您的时间计算中使用它。
回答by Robin
For geolocation you can use http://ipinfodb.com/, work great with an API!
对于地理定位,您可以使用http://ipinfodb.com/,使用API可以很好地工作!
function GeoLocation($precision='city',$ip,$api) {
if($precision == "country") {
$file = "ip_query_country.php";
}
else {
$file = "ip_query.php";
}
$params = @file_get_contents("http://api.ipinfodb.com/v2/ip_query.php?key=".$api."&ip=".$ip."&timezone=true");
$fields = @new SimpleXMLElement($params);
foreach($fields as $field => $val) {
$result[(string)$field] = (string)$val;
}
return $result;
}
回答by Lajos Veres
With a small "cheating" (using client side scripting) and posting the result to your server, you can access your client's timezone.
通过一个小的“欺骗”(使用客户端脚本)并将结果发布到您的服务器,您可以访问您客户端的时区。
For example you can use this javascript library:
例如,您可以使用这个 javascript 库:
https://bitbucket.org/pellepim/jstimezonedetect
https://bitbucket.org/pelepim/jstimezonedetect
var tz = jstz.determine(); // Determines the time zone of the browser client
tz.name(); // Returns the name of the time zone eg "Europe/Berlin"
回答by Philoxopher
The most reliable method is to ask the user to set his own timezone. I think it's a good idea to have a timestamp of when the users account was created in your own database for your own use.
最可靠的方法是要求用户设置自己的时区。我认为在您自己的数据库中创建用户帐户供您自己使用的时间戳是一个好主意。
These are the steps I would take to have a users timezone:
这些是我为拥有用户时区而采取的步骤:
1- Preserve the timestamp column that you have for your own usage
2- Add a new column for the actual users timezone
3- Use javascript to retrieve the timezone:new Date().getTimezoneOffset()/60;<br/>
4- Also allow users to set there own timezone as well
1- 保留您自己使用的时间戳列
2- 为实际用户时区添加一个新列
3- 使用 javascript 检索时区:new Date().getTimezoneOffset()/60;<br/>
4- 还允许用户设置自己的时区
You can also take it a step further and use a geo-location service to detect a users timezone.
您还可以更进一步,使用地理定位服务来检测用户时区。