javascript - 如何检测用户的时区?

标签 javascript php timezone

我需要根据用户的 IP 或 http header 了解我的用户当前所在的时区。

关于这个问题,我得到了很多答案,但我无法理解那些答案。有人说使用 -new Date().getTimezoneOffset()/60 ( from here )。但是这是什么意思?

我的 (index.php) 页面的根目录中有一个 date_default_timezone_set("Asia/Calcutta");。因此,为此我必须动态获取时区并将其设置为 Asia/Calcutta

最佳答案

用代码总结 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(); //For e.g.:"Asia/Kolkata" for the 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>

关于javascript - 如何检测用户的时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16525617/

相关文章:

php - 想要使用jquery获取选中复选框的所有值

机器之间Python的datetime strptime()不一致

javascript - 使用 Javascript 加载其他外部 Javascripts

javascript - 当值从 iframe 更改时范围不更新

javascript - 使用 AJAX 使用 JSON 填充选项卡

php - 使用未定义的常量项目 - 假定为 'project'(这将在未来的 PHP 版本中引发错误)

javascript - 如何比较 Moment.js 日期?

javascript - 如何在不触发事件的情况下保存数据

datetime - iCAL DTSTART 和 DTEND 格式混淆。应该很容易回答

database - 我可以在数据库中使用 Unix 时间作为复合主键吗?