php 如何获取客户的时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1905397/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:15:22  来源:igfitidea点击:

How to get client's timezone?

phpzend-frameworktimezone

提问by Valentin Golev

I'm writting an application using Zend_Framework (so the solution can rely on it).

我正在使用 Zend_Framework 编写应用程序(因此解决方案可以依赖它)。

How to get client's timezone?

如何获取客户的时区?

For example, if someone in Moscow, Russia, I want to get 3*60*60 (because there is UTC+3). If he is in UK, I want zero. If he uses UTC-3:30 (Canada?), I want -3.5*60*60.

例如,如果有人在俄罗斯莫斯科,我想得到 3*60*60(因为有 UTC+3)。如果他在英国,我想要零。如果他使用 UTC-3:30(加拿大?),我想要 -3.5*60*60。

(it's not a question about a format - I'm ok with getting 'Europe/Moscow' or 'UTC-3' for St. Petersburg, Russia, it's rather about getting timezone client uses. But delta in seconds is preferred)

(这不是关于格式的问题 - 我可以为俄罗斯圣彼得堡获取“欧洲/莫斯科”或“UTC-3”,而是获取时区客户端使用。但以秒为单位的增量是首选)

The only solution which comes to mind is letting javascript get the local time for me and redirect.

想到的唯一解决方案是让javascript为我获取本地时间并重定向。

采纳答案by Gordon

Check out this article on how to detect the timezoneby setting a Cookie through JavaScript that will hold the client's timezone. It's rather lenghty, but that is because it is quite verbose. I've implemented a solution along these lines in one of my own apps and it works quite well.

查看这篇文章,了解如何通过 JavaScript 设置 Cookie来检测时区,该 Cookie 将保存客户端的时区。它相当冗长,但那是因为它非常冗长。我已经在我自己的一个应用程序中实现了这些方面的解决方案,并且效果很好。

You could also send the timezone via Ajax to the server and have it do whatever you need to do it with then. Or, if you are not doing any serverside calculations with it, just apply the timezone client side where needed. Really depends on your usecase.

您还可以通过 Ajax 将时区发送到服务器,然后让它做您需要做的任何事情。或者,如果您没有使用它进行任何服务器端计算,只需在需要的地方应用时区客户端。真的取决于你的用例。

In addition to that, I suggest you let the visitor set his timezone himself and store that in the Cookie or a Session.

除此之外,我建议您让访问者自己设置时区并将其存储在 Cookie 或会话中。

回答by Westy92

I wrote a function using jQuery and PHP. This is tested, and does work!

我使用 jQuery 和 PHP 编写了一个函数。这是经过测试的,确实有效!

On the PHP page where you are want to have the timezone as a variable, have this snippet of code somewhere near the top of the page:

在您希望将时区作为变量的 PHP 页面上,将这段代码放在页面顶部附近的某处:

<?php    
    session_start();
    $timezone = $_SESSION['time'];
?>

This will read the session variable "time", which we are now about to create.

这将读取我们现在将要创建的会话变量“time”。

On the same page, in the , you need to first of all include jQuery:

在同一个页面中,您首先需要包含 jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

Also in the , below the jQuery, paste this:

同样在 jQuery 下方,粘贴以下内容:

<script type="text/javascript">
    $(document).ready(function() {
        if("<?php echo $timezone; ?>".length==0){
            var visitortime = new Date();
            var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
            $.ajax({
                type: "GET",
                url: "http://domain.com/timezone.php",
                data: 'time='+ visitortimezone,
                success: function(){
                    location.reload();
                }
            });
        }
    });
</script>

You may or may not have noticed, but you need to change the url to your actual domain.

您可能注意到也可能没有注意到,但是您需要将 url 更改为您的实际域。

One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this: (create a new file called timezone.phpand point to it with the above url)

最后一件事。您可能想知道 timezone.php 到底是什么。嗯,就是这样:(创建一个名为timezone.php的新文件,并用上面的 url 指向它)

<?php
    session_start();
    $_SESSION['time'] = $_GET['time'];
?>

If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.

如果这工作正常,它将首先加载页面,执行 JavaScript,然后重新加载页面。然后,您将能够读取 $timezone 变量并随意使用它!它返回当前的 UTC/GMT 时区偏移量 (GMT -7) 或您所在的任何时区。

回答by Lauber Bernhard

This is a cookie solution with very little overhead:

这是一个开销很小的 cookie 解决方案:

  echo <<<EOE
   <script type="text/javascript">
     if (navigator.cookieEnabled)
       document.cookie = "tzo="+ (- new Date().getTimezoneOffset());
   </script>
EOE;
  if (!isset($_COOKIE['tzo'])) {
    echo <<<EOE
      <script type="text/javascript">
        if (navigator.cookieEnabled) document.reload();
        else alert("Cookies must be enabled!");
      </script>
EOE;
    die();
  }
  $ts = new DateTime('now', new DateTimeZone('GMT'));
  $ts->add(DateInterval::createFromDateString($_COOKIE['tzo'].' minutes'));

回答by Myles

I think your best bet is to guess based on the Accept-Languageheader sent over by the client.

我认为您最好的选择是根据Accept-Language客户端发送的标头进行猜测。