PHP:
// 注意:php的时间是以秒算。js的时间以毫秒算 // 设置时区 date_default_timezone_set('PRC'); //配置每天的活动时间段 $starttimestr = date('Y-m-d H:i:s', strtotime(date('Y-m-d'))); $endtimestr = date('Y-m-d H:i:s', strtotime(date('Y-m-d', strtotime('+1 day')))); $starttime = strtotime($starttimestr); $endtime = strtotime($endtimestr); $nowtime = time(); if ($nowtime < $starttime) { exit("活动还没开始,活动时间是:{$starttimestr}至{$endtimestr}"); } if ($endtime >= $nowtime) { $lefttime = $endtime - $nowtime; //实际剩下的时间(秒) } else { $lefttime = 0; exit("活动已经结束!"); }
js:
var runtimes = 0; function GetRTime() { var lefttime = < ?php echo $lefttime; ? > * 1000 - runtimes * 1000; if (lefttime >= 0) { var nD = Math.floor(lefttime / (1000 * 60 * 60 * 24)) % 24; var nH = Math.floor(lefttime / (1000 * 60 * 60)) % 24; var nM = Math.floor(lefttime / (1000 * 60)) % 60; var nS = Math.floor(lefttime / 1000) % 60; document.getElementById("RemainD").innerHTML = nD; document.getElementById("RemainH").innerHTML = nH; document.getElementById("RemainM").innerHTML = nM; document.getElementById("RemainS").innerHTML = nS; if (lefttime == 5 * 60 * 1000) { alert("还有最后五分钟!"); } runtimes++; setTimeout("GetRTime()", 1000); } else { alert('活动结束了!'); location.reload(); } } var Num = 0; onload = function() { Refresh(); setInterval("Refresh();", 100); GetRTime(); } function Refresh() { if (Num < 10) { document.getElementById("RemainL").innerHTML = Num; Num = Num + 1; } else { Num = 0; } }