js活动倒计时代码
使用方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>倒计时-军少博客网-4dn.net</title>
</head>
<body> <?php $tiem='1603769505'?><?php //$tiem 结束时间 减 time() 当前时间?>
<div class="box" end-time="<?php echo $tiem?>" start-time="<?php echo time()?>">
距离活动结束还有<span id="time"></span>
</div>
<script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script>
//不足两位补0
function timeS(num){
return ('0'+num).slice(-2);
}
function FreshTime(){
//这两个是后台返回的时间戳,为了便于测试把开始时间变成当前本地的时间
var attr_s = $('.box').attr('start-time');
var attr_e = $('.box').attr('end-time');
var startTime = new Date().getTime();
var endTime = parseInt(attr_e*1000);
var differTime = endTime - startTime; //两时间差
var d = parseInt(differTime/(24*60*60*1000));
var h = parseInt(differTime/(60*60*1000)%24);
var m = parseInt(differTime/(60*1000)%60);
var s = parseInt(differTime/1000%60);
var t_html = d+"天"+timeS(h)+"时"+timeS(m)+"分"+timeS(s)+"秒";
$("#time").html(t_html);
if(differTime<=0){
$("#time").html('团购已结束');
clearInterval(timer);
}
}
FreshTime();
var timer = setInterval(FreshTime, 1000);
</script>
</body>
</html>
黑蜘蛛