Dan*_*Dan 26 javascript php timezone
我需要根据IP或http标头知道我的用户目前在哪个时区.
关于这个问题,我得到了很多答案,但我无法理解这些答案.有人说使用-new Date().getTimezoneOffset()/60(从这里).但是这是什么意思?
我date_default_timezone_set("Asia/Calcutta");在(index.php)页面的根目录中有一个.因此,我必须动态获取时区并将其设置为代替Asia/Calcutta.
Ela*_*Lee 35
总结Matt Johnson在代码方面的答案:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
var tz = jstz.determine(); // Determines the time zone of the browser client
var timezone = tz.name(); //'Asia/Kolhata' for Indian Time.
$.post("url-to-function-that-handles-time-zone", {tz: timezone}, function(data) {
//Preocess the timezone in the controller function and get
//the confirmation value here. On success, refresh the page.
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
Mat*_*int 23
浏览器的时区信息不是HTTP规范的一部分,因此您无法从标头中获取它.
如果您有位置坐标(例如,来自移动设备GPS),则可以使用以下方法之一找到时区.但是,通过IP地址进行地理定位不是一个很好的解决方案,因为IP通常是ISP或代理服务器的IP,可能位于另一个时区.
您可以使用一些策略来尝试检测时区,例如使用jsTimeZoneDetect库,这是一个很好的起点,但不够完善,您不能仅仅依靠它.如果你正在使用moment.js,那么在time-timezone中有一个内置函数,moment.tz.guess()它可以做同样的事情.
使用JavaScript getTimezoneOffset()函数的想法存在缺陷,因为您没有获得时区 - 只是特定日期的单个偏移量.请参阅TimeZone标记wiki的标题为"TimeZone!= Offset"的部分.
无论你怎么看,最终你必须决定两种方法之一:
要么
我在这个答案中更详细地讨论了这个问题(来自ac#perspective).
| 归档时间: |
|
| 查看次数: |
50723 次 |
| 最近记录: |