Home>Article>Backend Development> PHP instant kill countdown example accurate to milliseconds

PHP instant kill countdown example accurate to milliseconds

little bottle
little bottle forward
2019-04-22 16:32:08 3192browse

The main content of this article is about introducing PHP's flash kill countdown that is accurate to milliseconds. It has certain reference value. Interested friends should come and learn about it.

Accurate to milliseconds, instant kill countdown PHP source code example, the front-end js activity displays the countdown, and the background calculates the countdown time. The activity countdown time is regularly refreshed every 0.1 seconds.

PHP:


1 // 注意:php的时间是以秒算。js的时间以毫秒算 2 // 设置时区 3 date_default_timezone_set('PRC'); 4 //配置每天的活动时间段 5 $starttimestr = date('Y-m-d H:i:s', strtotime(date('Y-m-d'))); 6 $endtimestr = date('Y-m-d H:i:s', strtotime(date('Y-m-d', strtotime('+1 day')))); 7 $starttime = strtotime($starttimestr); 8 $endtime = strtotime($endtimestr); 9 $nowtime = time(); 10 if ($nowtime < $starttime) { 11 exit("活动还没开始,活动时间是:{$starttimestr}至{$endtimestr}"); 12 } 13 if ($endtime >= $nowtime) { 14 $lefttime = $endtime - $nowtime; //实际剩下的时间(秒) 15 } else { 16 $lefttime = 0; 17 exit("活动已经结束!"); 18 }

Related tutorials:PHP video tutorial

js:


1 var runtimes = 0; 2 function GetRTime() { 3 var lefttime = < ?php echo $lefttime; ? > * 1000 - runtimes * 1000; 4 if (lefttime >= 0) { 5 var nD = Math.floor(lefttime / (1000 * 60 * 60 * 24)) % 24; 6 var nH = Math.floor(lefttime / (1000 * 60 * 60)) % 24; 7 var nM = Math.floor(lefttime / (1000 * 60)) % 60; 8 var nS = Math.floor(lefttime / 1000) % 60; 9 document.getElementById("RemainD").innerHTML = nD; 10 document.getElementById("RemainH").innerHTML = nH; 11 document.getElementById("RemainM").innerHTML = nM; 12 document.getElementById("RemainS").innerHTML = nS; 13 if (lefttime == 5 * 60 * 1000) { 14 alert("还有最后五分钟!"); 15 } 16 runtimes++; 17 setTimeout("GetRTime()", 1000); 18 } else { 19 alert('活动结束了!'); 20 location.reload(); 21 } 22 } 23 var Num = 0; 24 onload = function() { 25 Refresh(); 26 setInterval("Refresh();", 100); 27 GetRTime(); 28 } 29 function Refresh() { 30 if (Num < 10) { 31 document.getElementById("RemainL").innerHTML = Num; 32 Num = Num + 1; 33 } else { 34 Num = 0; 35 } 36 }

Related tutorials:js video tutorial

The above is the detailed content of PHP instant kill countdown example accurate to milliseconds. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete