您的位置:首页 > Web前端 > JavaScript

简单实用的JS倒计时

2017-12-27 10:15 106 查看
方法一:
<?php
//php的时间是以秒算。js的时间以毫秒算
date_default_timezone_set("Asia/Hong_Kong");//地区
//配置每天的活动时间段
$starttimestr = "2017-07-17 11:00:00";
$endtimestr = "2017-07-17 13:00:00";
$starttime = strtotime($starttimestr);
$endtime = strtotime($endtimestr);
$nowtime = time();
if ($nowtime<$starttime){
die("活动还没开始,活动时间是:每周四下午{$starttimestr}至{$endtimestr}");
}
$lefttime = $endtime-$nowtime; //实际剩下的时间(秒)
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP实时倒计时!</title>
<script language="JavaScript">
var runtimes = 0;
function GetRTime(){
var nMS = <?=$lefttime?>*1000-runtimes*1000;
var nH=Math.floor(nMS/(1000*60*60))%24;
alert(nH);
var nM=Math.floor(nMS/(1000*60)) % 60;
var nS=Math.floor(nMS/1000) % 60;
document.getElementById("RemainH").innerHTML=nH;
document.getElementById("RemainM").innerHTML=nM;
document.getElementById("RemainS").innerHTML=nS;
if(nMS>6*59*60*1000&&nMS<=6*60*60*1000)
{
//三分钟前不能购买
alert("还有最后五分钟!停止购买");
return false;
}
runtimes++;
setTimeout("GetRTime()",1000);
}
window.onload=GetRTime;
// -->
</script>
</head>
<body>
<h1>开奖时间剩余:<strong id="RemainH">XX</strong>:<strong id="RemainM">XX</strong>:<strong id="RemainS">XX</strong></h1>

方法二:

<?php
$old_tiem = "2017-07-20 15:00:00";
$tiem = strtotime($old_tiem);

$strt = $tiem-time();
echo '截止时间:'.date('Y-m-d H:i:s',$tiem).'<p>';
echo '当前时间:'.date('Y-m-d H:i:s',time

()).'<p>';
echo '倒计时:'.date('Y-m-d H:i:s',$strt).'<p>';

?>
<div class="time">
<span id="t_d">00天</span>
<span id="t_h">00时</span>
<span id="t_m">00分</span>
<span id="t_s">00秒</span>
</div>
<script>
function GetRTime(){
var EndTime= new Date('<?php echo date

('Y-m-d H:i:s',$tiem) ?>');
var NowTime = new Date();
var t =EndTime.getTime() -

NowTime.getTime();
var d=0;
var h=0;
var m=0;
var s=0;
if(t>=0){
d=Math.floor(t/1000/60/60/24);
h=Math.floor(t/1000/60/60%24);
m=Math.floor(t/1000/60%60);
s=Math.floor(t/1000%60);
}
document.getElementById("t_d").innerHTML =

d + "天";
document.getElementById("t_h").innerHTML =

h + "时";
document.getElementById("t_m").innerHTML =

m + "分";
document.getElementById("t_s").innerHTML =

s + "秒";
}
setInterval(GetRTime,0);

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: