!DOCTYPE html>
html>
head>
title>PHP實現(xiàn)倒計時功能/title>
/head>
body>
?php
date_default_timezone_set('PRC');
$starttimestr = date('Y-m-d H:i:s', strtotime('now'));
$starttimestr= mktime(0,0,0,7,7,2020);
//$endtimestr = date('Y-m-d H:i:s', strtotime('+18 hours 1 minutes'));
$endtimestr = mktime(18,0,0,date('m'),date('d'),date('Y'));
$starttime = strtotime($starttimestr);
$endtime = strtotime($endtimestr);
$nowtime = time();
if ($nowtime $starttime) {
exit("考試已經結束了,考試時間是:{$starttimestr}至{$endtimestr}");
}
if ($endtime >= $nowtime) {
$lefttime = $endtime - $nowtime; //實際剩下的時間(秒)
} else {
$lefttime = 0;
exit("考試已經結束!");
}
?>
p>考試開始時間:?php echo $starttimestr; ?>/p>
p>考試結束時間:?php echo $endtimestr; ?>/p>
p>
距離考試結束還有:
span id="RemainD">/span> 天
span id="RemainH">/span> 小時
span id="RemainM">/span> 分鐘
span id="RemainS">/span> 秒
/p>
script type="text/javascript">
var runtimes = 0;
function GetRTime() {
var lefttime = ?php echo $lefttime; ?> * 1000 - runtimes * 1000;
if (lefttime >= 0) {
var nD = s.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;
runtimes++;
setTimeout("GetRTime()", 1000);
} else {
alert('考試結束了!');
location.reload();
}
}
onload = function() {
GetRTime();
}
/script>
/body>
/html>