温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么实现精确到毫秒秒杀倒计时

发布时间:2021-06-09 16:49:10 来源:亿速云 阅读:195 作者:Leah 栏目:开发技术

这篇文章给大家介绍使用PHP怎么实现精确到毫秒秒杀倒计时,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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怎么实现精确到毫秒秒杀倒计时就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI