php 如何使用jquery获取用户时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8090549/
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 get user timezone using jquery?
提问by user967144
I would like to get their timezone with names like 'Asia/Calcutta'or 'Australia/Darwin'
我想用“亚洲/加尔各答”或“澳大利亚/达尔文”等名称来获取他们的时区
I have php installed on myserver and its time set to 'UTC'
我在我的服务器上安装了 php 并且它的时间设置为“UTC”
回答by Scott
This is my first post here but hopefully I can help someone. I felt compelled because this site has helped me a lot. Thanks stackoverflow members!
这是我在这里的第一篇文章,但希望我能帮助别人。我感到有压力,因为这个网站对我帮助很大。感谢 stackoverflow 成员!
http://www.pageloom.com/automatic-timezone-detection-with-javascript
http://www.pageloom.com/automatic-timezone-detection-with-javascript
From what I understand about this JavaScript code it is very accurate and will be able to return the offset from UST, the corresponding Olson Database timezone name, and handle the daylight savings time issue (ex. -5:00, America/New_york, true).
根据我对这段 JavaScript 代码的理解,它非常准确,并且能够返回 UST 的偏移量、相应的 Olson 数据库时区名称,并处理夏令时问题(例如 -5:00、America/New_york、true )。
http://www.php.net/manual/en/timezones.america.php
http://www.php.net/manual/en/timezones.america.php
The above link is just a list of the php supported timezone parameters, which as far as I can tell align with this JavaScript code (I am sure that is beneficial I just don't know why yet I am still writing my code and I am kinda new to php/mysql).
上面的链接只是一个 php 支持的时区参数的列表,据我所知,它与这段 JavaScript 代码保持一致(我确信这是有益的,我只是不知道为什么我还在写我的代码,我是对 php/mysql 有点新)。
The only hurdle you will face after getting this code working on your html page will likely be getting these values to php and then to mysql if that is what you need. I achieved this by sending these values as a $.post using JQuery. I think this is the easiest way to do it (please someone correct me if I am wrong). I believe the other alternatives are using an AJAX command (more complicated) or cookies(I think?).
在您的 html 页面上运行此代码后,您将面临的唯一障碍很可能是将这些值传递给 php,然后传递给 mysql(如果这是您需要的)。我通过使用 JQuery 将这些值作为 $.post 发送来实现了这一点。我认为这是最简单的方法(如果我错了,请有人纠正我)。我相信其他替代方案是使用 AJAX 命令(更复杂)或 cookie(我认为?)。
After I got the values from the Java-script code to the server (PHP) I stored them as session variables so they would change if the user on my site logged in from a different timezone then usual. However they could easily be saved to the database as well.
在我将 Java 脚本代码中的值发送到服务器 (PHP) 后,我将它们存储为会话变量,这样如果我网站上的用户从与平常不同的时区登录,它们就会发生变化。然而,它们也可以很容易地保存到数据库中。
If you use the JQuery route this needs to be in the header of the html document where the $.post is being executed.
如果您使用 JQuery 路由,这需要位于执行 $.post 的 html 文档的标题中。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
The most updated timezonedetect script can be found linked on that first url I posted.
可以在我发布的第一个 url 上找到最新的 timezonedetect 脚本。
//replace this comment with the most updated timezonedetect code from that first link
var timezone = jstz.determine_timezone();
var tzoffset = timezone.offset();
var tzname = timezone.name();
var tzdst = timezone.dst();
$.post("scripts/tzdetect.php", { tzoffset: tzoffset, tzname: tzname, tzdst: tzdst } );
The receiving file in this case tzdetect.php should look like this if you want the data stored as session variables.
如果您希望将数据存储为会话变量,则在这种情况下的接收文件 tzdetect.php 应该如下所示。
<?php
session_start();
$_SESSION['tzname'] = $_POST['tzname'];
$_SESSION['tzoffset'] = $_POST['tzoffset'];
$_SESSION['tzdst'] = $_POST['tzdst'];
?>
回答by ValeriiVasin
回答by Jon Skeet
Your question is really about Javascript rather than PHP - because it depends on the client capabilities.
您的问题实际上是关于 Javascript 而不是 PHP - 因为它取决于客户端的功能。
As I understand it, Javascript allows you to determine the current time zone offset (the difference between UTC and local time) but that does notallow you to unambiguously determine the time zone. Many time zones will share the same offset at any one particular point in time, but will differ at other times, in terms of whether they observe daylight saving and at what point they change into and out of daylight saving.
据我了解,JavaScript允许你确定当前时区偏移量(UTC与本地时间之间的区别),但是这并没有让你明确地确定的时区。许多时区将在任何一个特定时间点共享相同的偏移量,但在其他时间会有所不同,这取决于它们是否遵守夏令时以及在什么时候进入和退出夏令时。
If you definitely need the time zone, you might want to work out a list of possibletime zones, and present that to the user - also allowing them to override the narrowing decision, choosing anytime zone.
如果您确实需要时区,您可能需要制定一个可能的时区列表,并将其呈现给用户 - 还允许他们覆盖缩小范围的决定,选择任何时区。
Again, it really depends on what you're trying to achieve.
同样,这实际上取决于您要实现的目标。
回答by Tadeck
You do not need jQuery to do this. Try the following code:
您不需要 jQuery 来执行此操作。试试下面的代码:
var offset = (new Date()).getTimezoneOffset();
alert(offset);
It will alert current timezone offset from the browser(in minutes).
它将提醒当前时区与浏览器的偏移量(以分钟为单位)。
I think you will need to convert it to the timezone name on your own. If you want the names to be consistent, you should not depend on the user's browser.
我认为您需要自行将其转换为时区名称。如果您希望名称一致,则不应依赖于用户的浏览器。
You can use this code (see this jsfiddle):
您可以使用此代码(请参阅此 jsfiddle):
var offset = (new Date()).getTimezoneOffset();
var timezones = {
'-12': 'Pacific/Kwajalein',
'-11': 'Pacific/Samoa',
'-10': 'Pacific/Honolulu',
'-9': 'America/Juneau',
'-8': 'America/Los_Angeles',
'-7': 'America/Denver',
'-6': 'America/Mexico_City',
'-5': 'America/New_York',
'-4': 'America/Caracas',
'-3.5': 'America/St_Johns',
'-3': 'America/Argentina/Buenos_Aires',
'-2': 'Atlantic/Azores',
'-1': 'Atlantic/Azores',
'0': 'Europe/London',
'1': 'Europe/Paris',
'2': 'Europe/Helsinki',
'3': 'Europe/Moscow',
'3.5': 'Asia/Tehran',
'4': 'Asia/Baku',
'4.5': 'Asia/Kabul',
'5': 'Asia/Karachi',
'5.5': 'Asia/Calcutta',
'6': 'Asia/Colombo',
'7': 'Asia/Bangkok',
'8': 'Asia/Singapore',
'9': 'Asia/Tokyo',
'9.5': 'Australia/Darwin',
'10': 'Pacific/Guam',
'11': 'Asia/Magadan',
'12': 'Asia/Kamchatka'
};
alert(timezones[-offset / 60]);
However I would not rely on this too much.
不过我不会太依赖这个。
回答by abhinav
You can get the timezone offset. Which you can use to calculate the approximate timezone. You can keep an associative list of timezone offset and the names
.
This method does not give any exact values, though.
您可以获得时区偏移量。您可以使用它来计算大致的时区。您可以保留时区偏移量和names
. 但是,此方法不会给出任何确切值。
回答by Santhosh J
<?
echo date("e");
?>
This will give the output:
这将给出输出:
Asia/Kolkata
亚洲/加尔各答