Javascript 如何获取用户的本地时间而不是服务器的时间?

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

How can I get the user's local time instead of the server's time?

phpjavascript

提问by Simon

How can I get the time of the client side? When I use date()it returns server's time.

如何获取客户端的时间?当我使用date()它时返回服务器的时间。

回答by phimuemue

Here's a "PHP" solution:

这是一个“PHP”解决方案:

echo '<script type="text/javascript">
var x = new Date()
document.write(x)
</script>';

回答by Russell Dias

As mentioned by everyone PHP only displays server side time.

正如大家所提到的,PHP 只显示服务器端时间。

For client side, you would need Javascript, something like the following should do the trick.

对于客户端,您需要 Javascript,如下所示应该可以解决问题。

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

if (minutes < 10) {
    minutes = "0" + minutes;
}

document.write("<b>" + hours + ":" + minutes + " " + "</b>");

And if you want the AM/PM suffix, something like the following should work:

如果您想要 AM/PM 后缀,则应该可以使用以下内容:

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

var suffix = "AM";

if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
}

if (hours == 0) {
    hours = 12;
}

if (minutes < 10) {
    minutes = "0" + minutes;
}

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");

Here is a list of additional JavaScript Dateand Timefunctionsyou could mess around with.

这是您可能会遇到的其他 JavaScriptDateTime函数的列表

回答by Jamescun

You could possibly use Geolocation by IP Address to work out which country the user is in, and then use that.

您可以使用 IP 地址地理定位来确定用户所在的国家/地区,然后使用它。

But using Javascript or letting the user choose a Timezone will probably be better.

但是使用 Javascript 或让用户选择时区可能会更好。

回答by Pascal MARTIN

As PHP runs on the server-side, you cannot access the client-side time from PHP: PHP doesn't know much about the browser -- and you can have PHP scripts that run without being called from a browser.

由于 PHP 在服务器端运行,因此您无法从 PHP 访问客户端时间:PHP 对浏览器了解不多——您可以运行 PHP 脚本,而无需从浏览器调用。

But you could get it from Javascript(which is executed on the client-side), and, then, pass it to PHP via an Ajax request, for example.

但是您可以从 Javascript (在客户端执行)获取它,然后通过 Ajax 请求将它传递给 PHP,例如。


And here are a couple of questions+answers that might help you getting started :


这里有几个问题+答案可能会帮助您入门:

回答by phimuemue

PHP is server side only as far as i know.

据我所知,PHP 只是服务器端。

You maybe want to use JavaScript.

您可能想使用 JavaScript。

回答by Gadget Guru

As other's have mentioned, you can use Geo Location Services based on the IP Address.

正如其他人提到的,您可以使用基于 IP 地址的地理定位服务。

I found it to be off by about 18 seconds due to IP location accuracy, by using a $responseTime offset it helped me narrow it down to 2 second accuracy in the Viewers Location.

由于 IP 位置的准确性,我发现它关闭了大约 18 秒,通过使用 $responseTime 偏移,它帮助我将其缩小到查看者位置中的 2 秒准确度。

  <?php; 

   echo deviceTime('D, M d Y h:i:s a');

   function deviceTime($dateFormatString)
   { 
        $responseTime = 21;
        $ip = (isset($_SERVER["HTTP_CF_CONNECTING_IP"])?$_SERVER["HTTP_CF_CONNECTING_IP"]:$_SERVER['REMOTE_ADDR']);
        $ch = file_get_contents('https://ipapi.co/'.$viewersIP.'/json/');
        $ipParts = json_decode($ch,true);
        $timezone = $ipParts['timezone'];
        $date = new DateTime(date('m/d/Y h:i:s a', time()+$responseTime));
        $date->setTimezone(new DateTimeZone($timezone));
    return $date->format($dateFormatString);
   }
   ?>