PHP+JS動的表示サーバー時間、ローカル時間

1985 ワード

サーバー時間+ローカル時間をWebページに動的に表示します.コードは次のとおりです.






var timeDiff=new Date().valueOf()-<?php echo time()*1000;?>;
function serverTime(){
        this.date = new Date();
        date.setTime(new Date().valueOf()-timeDiff);
        this.year        =date.getFullYear();
        this.month        =date.getMonth()+1;
        this.day        =date.getDate();
        this.hour        =date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        this.minute =date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        this.second =date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        var s=year+' '+month+' '+day+'  '+hour+':'+minute+':'+second;
        document.getElementById("serverTime").innerHTML=s;
}
function localTime(){
        this.date = new Date();
        this.year        =date.getFullYear();
        this.month        =date.getMonth()+1;
        this.day        =date.getDate();
        this.hour        =date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        this.minute =date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        this.second =date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        var s=year+' '+month+' '+day+'  '+hour+':'+minute+':'+second;
        document.getElementById("localTime").innerHTML=s;
}
window.οnlοad=function(){
        serverTime();
        localTime();        
        setInterval(function(){
                serverTime();
                localTime();
        }, 1000);
}