PHP实现倒计时功能

wqcong 2020-11-16

本文实例为大家分享了PHP实现倒计时功能的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
<head>
 <title>PHP实现倒计时功能</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>

相关推荐