我正在尝试在启动时获取服务器时间并更新它,因为我要实时地调整一些元素。问题是,如果我的serverTime
没有T,那么firefox和IE上的时间是NaN,但是如果我用chrome和IE上的T替换空白空间我的时间错了。
我现在用T吸收替换白色空间的解决方法,但是时间不多了:)
谢谢大家
开始时:
$tmpTime = date("Y-m-d H:i:s");
目:
<script>
var serverTime = '<?=$tmpTime?>';
serverTime = serverTime.replace(" ", "T");
</script>
脚本:
setInterval(function () {
console.log(serverTime);
var tmpTime = new Date(serverTime);
console.log(tmpTime);
var t = tmpTime.getTime();
t = t + 1000;
tmpTime = new Date(t);
serverTime = t;
if (tmpTime.getMinutes() < 10) {
var minutes = "0" + tmpTime.getMinutes();
} else {
var minutes = tmpTime.getMinutes();
};
newTime = tmpTime.getHours() + ":" + minutes;
$('#liveTime').text(newTime);
if ($("#program li[time-id='" + newTime + "'][class='alert']").length !== 0) {
alert("Lo streaming da te programmato sta per iniziare!");
$("#program li[time-id='" + newTime + "'][class='alert']").removeClass("alert");
}
titleToShow();
}, 1000);
function titleToShow() {
$("#program li").each(function () {
var prevTime = $(this).prev("li").attr("time-id");
var thisTime = $(this).attr("time-id");
var nextTime = $(this).next("li").attr("time-id");
currentTime = Date.parse('01/01/2011 ' + newTime);
prevTime = Date.parse('01/01/2011 ' + prevTime);
nextTime = Date.parse('01/01/2011 ' + nextTime);
thisTimeNew = Date.parse('01/01/2011 ' + thisTime);
if (currentTime >= thisTimeNew && currentTime < nextTime && currentTime > prevTime) {
title = $(this).find("p").text();
if (title != $("p#playingTitle").text()) {
$("p#playingTitle").text(title);
}
}
})
}
答案 0 :(得分:1)
不要使用格式化日期,只需将Unix时间戳值传递给脚本(不要忘记将它乘以1000,因为JS的工作时间为毫秒)。
var serverTime = <?php echo time(); ?>;
var tmpTime = new Date(serverTime * 1000);