我有一个以 UTC 格式获取时间的 JavaScript 时钟,它目前可以工作,但时间是基于客户端计算机的。我可以做些什么来代替服务器的时间?我使用 PHP 作为服务器脚本语言。我不想使用 AJAX。
<?php
$year = date("Y");
$month = date("m");
$day = date("d");
$hour = date("h");
$minute = date("i");
$str = $year . $month . $day . $hour . $minute;
echo "history.pushState('', 'title', '?q=$str');";
echo "var ct = '$str';";
?>
function dT(){
var d = new Date();
d = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
d.setTime(d.getTime());
v = d.getFullYear() + "" +
padstr(d.getMonth()) + "" +
padstr(d.getDate()) + "" +
padstr(d.getHours()) + "" +
padstr(d.getMinutes()) + "" + padstr(d.getSeconds());
if(ct !== v){
history.pushState('', 'title', '?q=' + v);
ct = v;
}
setTimeout('dT()', 1000);
}
dT();
编辑
var a = moment().format("<?php echo date("Y-m-d H:i:s", time()); ?>");
document.getElementById("time").innerHTML = a;
function clock_tick(){
var time = moment(a);
time.add('second', 1);
a = time.format("YYYY-MM-DD HH:MM:SS");
document.getElementById("time").innerHTML = a;
setTimeout("clock_tick()", 1000);
}
clock_tick();