如何获取用户的本地时间而不是服务器的时间? [英] How can I get the user's local time instead of the server's time?

查看:160
本文介绍了如何获取用户的本地时间而不是服务器的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

正如每个人所提到的,PHP只显示服务器端时间。

As mentioned by everyone PHP only displays server side time.

对于客户端,你需要使用Javascript,类似下面的内容应该可以解决问题。

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

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

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

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

如果您想要AM / PM后缀,则以下内容应该有效:

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

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>");

这是一系列额外的JavaScript 日期时间功能你可能会搞乱周围有。

Here is a list of additional JavaScript Date and Time functions you could mess around with.

这篇关于如何获取用户的本地时间而不是服务器的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆