有没有人知道如何让实时的javascript时间运行..
我有这个PHP代码 $expiredate = date('d m Y G:i:s', $rdate1);
$f_ex_date = explode(" ", $expiredate);
$f_ex_time = explode(":", $expiredate);
$_endDate = mktime($f_ex_date[0],$f_ex_date[1],$f_ex_date[2],$f_ex_date[1],$f_ex_date[0],$f_ex_date[2]);
$time = $_endDate - time();
$days = floor($time/86400);
$hours = floor(($time-($days*86400))/3600);
$mins = floor (($time-($days*86400)-($hours*3600))/60);
$secs = floor ($time-($days*86400)-($hours*3600)-($mins*60));
echo "Your account going to be expired in <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$days."</span> Days <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$hours."</span> Hours <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$mins."</span> Minutes <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$secs."</span> Seconds";
是否可以让它像现场直播?
答案 0 :(得分:12)
这是怎么做的。 Working Demo
首先,在HTML文档的顶部:
.datetime {
color: #C11B17;
font-family:arial;
font-size: 16px;
}
我们这样做,所以我们可以稍微清理一下我们的HTML代码:
$rdate1 = 1240550032; // Fri, 24 Apr 2009 05:13:52 GMT
$expiredate = date('d m Y G:i:s', $rdate1);
$time = $rdate1 - time();
$days = floor($time/86400);
$hours = floor(($time-($days*86400))/3600);
$mins = floor(($time-($days*86400)-($hours*3600))/60);
$secs = floor($time-($days*86400)-($hours*3600)-($mins*60));
printf("
Your account is going to expire in
<span class='datetime' id='days'>%s</span> Days
<span class='datetime' id='hours'>%s</span> Hours
<span class='datetime' id='minutes'>%s</span> Minutes
<span class='datetime' id='seconds'>%s</span> Seconds
", $days, $hours, $mins, $secs);
我不太确定你采取的中间步骤来自哪里,但上面的代码让我得到$rdate1
(可能是一个unix时间戳)和time()
之间的时间差。 / p>
最后,我们可以使用Javascript执行类似的操作,以便在页面加载后更新时间:
addEvent(window, 'load', function() {
var eDays = document.getElementById('days');
var eHours = document.getElementById('hours');
var eMinutes = document.getElementById('minutes');
var eSeconds = document.getElementById('seconds');
var timer;
timer = setInterval(function() {
var vDays = parseInt(eDays.innerHTML, 10);
var vHours = parseInt(eHours.innerHTML, 10);
var vMinutes = parseInt(eMinutes.innerHTML, 10);
var vSeconds = parseInt(eSeconds.innerHTML, 10);
vSeconds--;
if(vSeconds < 0) {
vSeconds = 59;
vMinutes--;
if(vMinutes < 0) {
vMinutes = 59;
vHours--;
if(vHours < 0) {
vHours = 23;
vDays--;
}
}
} else {
if(vSeconds == 0 &&
vMinutes == 0 &&
vHours == 0 &&
vDays == 0) {
clearInterval(timer);
}
}
eSeconds.innerHTML = vSeconds;
eMinutes.innerHTML = vMinutes;
eHours.innerHTML = vHours;
eDays.innerHTML = vDays;
}, 1000);
});
function addEvent( obj, type, fn ) {
if ( obj.attachEvent ) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
} else
obj.addEventListener( type, fn, false );
}
答案 1 :(得分:4)
使用一点JavaScript在客户端上可行。如果不使用像jQuery这样的框架,这将是一个很小的帮助,基本方法将类似于以下内容:
在事件处理程序中:
作为一个具体示例,以下函数将使用命名元素设置简单的日期/时间更新:
function clock( id ) {
var target = document.getElementById( id );
if( target ) {
var callback = function() {
var datetime = new Date().toLocaleString();
target.innerHTML = datetime;
};
callback();
window.setInterval( callback, 1000 );
}
}
请注意使用new Date().toLocaleString()
检索并格式化当前日期/时间;另外,使用window.setInterval()
来设置每秒触发的回调。
答案 2 :(得分:1)
PHP,因为它是 server-side ,无法生效。您必须进行日期操作(至少是更改的内容)并使用JavaScript更新DOM,即 client-side 。
史蒂夫
答案 3 :(得分:0)
JS-Clock是直播时钟的最佳解决方案。它的迷你JS版只有4 KB。我推荐这个。