dongxun5349 2015-03-26 17:42
浏览 90
已采纳

显示服务器时间,包括时区偏移量

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

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

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.

  • 写回答

3条回答 默认 最新

  • dougan4884 2015-03-26 17:56
    关注

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

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

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

    Your JS code should be

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题