首页 > 语言 > PHP > 正文

PHP精确到毫秒秒杀倒计时实例详解

2024-05-05 00:07:27
字体:
来源:转载
供稿:网友

PHP,秒杀,倒计时

精确到毫秒秒杀倒计时PHP源码实例,前台js活动展示倒计时,后台计算倒计时时间。每0.1秒定时刷新活动倒计时时间。

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;   } }

以上所述是小编给大家介绍的PHP精确到毫秒秒杀倒计时实例详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对VeVb武林网网站的支持!


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选