显示服务器时间,包括时区偏移 [英] show server time including timezone offset

查看:35
本文介绍了显示服务器时间,包括时区偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 php 和 js 显示服务器的实际时间时遇到了一些困难.
在服务器端,我有以下 php 代码:

i have some difficulties showing the actual time of the server using php and js.
on the server-side i have following php code:

$date = new DateTime();
echo $date->getTimestamp();

在客户端,如果有以下js代码改变div的内容以显示当前时间:

on the client-side if have the following js code that changes the content of a div to display the current time:

flag = true;
timer = '';


function clock()
{
        if ( flag ) {
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
            xmlhttp.send();

            var stamp = xmlhttp.responseText;


            timer = stamp*1000;
        }
        var d = new Date(timer);

        var hours = d.getHours();
        var minutes = d.getMinutes();
        var seconds = d.getSeconds();

        //hours = hours % 12;
        //hours = hours ? hours : 12; // the hour ’0' should be ’12'
        minutes = minutes < 10 ? '0'+minutes : minutes;
        seconds = seconds < 10 ? '0'+seconds : seconds;
        var strTime = hours + ':' + minutes + ':' + seconds;
        document.getElementById("clock").innerHTML= strTime ;

        flag = false;
        timer = timer + 1000;
}

window.onload = function() {
    setInterval(clock, 1000);
};

只要服务器的时区和我的时区相同,就可以工作.但是一旦我更改了服务器上的时区,它就不再起作用了.尽管服务器上的 bash 命令 date 以正确的偏移量显示时间,但它仍然会显示我的本地客户端时间.

this works as long as the timezone of the server and mine are the same. but as soon as i change the timezone on the server, it doesn't work anymore. it still will show my local client time although the bash command date on the server shows the time in the right offset.

我该如何解决这个问题?我真的需要显示服务器本地时间.

how do i fix this? i really need to show the server-local time.

推荐答案

You are sent in unix timestamp from PHP可能正在使用

You are sending in unix timestamp from PHP must be probably using

echo date("Y-m-d H:i:s", time());

如果你想使用字符串来使用JS创建日期对象.

if you want to use the string for creating date object using JS.

你的JS代码应该是

function clock()
{
    if ( flag ) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
        xmlhttp.send();

        var stamp = xmlhttp.responseText;

        var timer = new Date(stamp);
    }
    var d = new Date(timer);

    var hours = d.getHours();
    var minutes = d.getMinutes();
    var seconds = d.getSeconds();

    //hours = hours % 12;
    //hours = hours ? hours : 12; // the hour ’0' should be ’12'
    minutes = minutes < 10 ? '0'+minutes : minutes;
    seconds = seconds < 10 ? '0'+seconds : seconds;
    var strTime = hours + ':' + minutes + ':' + seconds;
    document.getElementById("clock").innerHTML= strTime ;

    flag = false;
    timer = new Date(timer.getTime() + 1000);
}

这篇关于显示服务器时间,包括时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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