【PHP学习】倒计时程序例子

这个在商品的地方或者其他有时效性的东西上用得着。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

//php的时间是以秒算。js的时间以毫秒算

date_default_timezone_set("Asia/Hong_Kong");//地区

//配置每天的活动时间段
$starttimestr = "09:00:00";
$endtimestr = "23:50:00";

$starttime =  strtotime($starttimestr);
$endtime  =   strtotime($endtimestr);
$nowtime  =   time();

if ($nowtime<$starttime){
die("活动还没开始,活动时间是:{$starttimestr}{$endtimestr}");
}
$lefttime = $endtime-$nowtime;  //实际剩下的时间(秒)    ///在实际开发中,这个$lefttime可以在PHP函数中实现,包含在返回的特价商品数组中
?>

前台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//前台页面代码
<!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 = <?php echo $lefttime ?>*1000-runtimes*1000;

var nD=Math.floor(nMS/(1000*60*60*24))%365;    //获取天数
var nH=Math.floor(nMS/(1000*60*60))%24;          //获取小时数
var nM=Math.floor(nMS/(1000*60)) % 60;          ////获取分钟
var nS=Math.floor(nMS/1000) % 60;                   ////获取秒数



document.getElementById("RemainD").innerHTML=nD;
document.getElementById("RemainH").innerHTML=nH;
document.getElementById("RemainM").innerHTML=nM;
document.getElementById("RemainS").innerHTML=nS;
if(nMS>5*59*1000&&nMS<=5*60*1000)
{
 alert("还有最后五分钟!");
}
runtimes++;
setTimeout("GetRTime()",1000);
}
window.onload=GetRTime;
// -->
</script>
</head>
<body>
  <h1>剩余<strong id="RemainD">XX</strong><strong id="RemainH">XX</strong>小时<strong id="RemainM">XX</strong><strong id="RemainS">XX</strong><h1>
</body>
</html>

更简单的php倒计时

1
2
3
4
5
6
7
8
9
10
11
<?php
$date1=strtotime('2008-08-08');  //把日期转换成时间戳
$date2=time(); //取当前时间的时间戳
             
$nowtime=strftime("%y年-%m月-%d日",$date2); //格式化输出日期
             
$days=round(($date1-$date2)/3600/24);  //四舍五入
             
echo "今天是<font color="red">".$nowtime."</font>";
echo "<br />距北京2008年奥运会还有<font color="red">".$days."</font>天";        
?>

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
function countdown($unix_timestamp) {
$date = $unix_timestamp-time();
$day = $date/60/60/24;
$days = (int)$day;
$hour = $date/60/60 - $days*24;
$hours = (int)$hour;
$minute = $date/60 - $days*24*60 - $hours*60;
$minutes = (int)$minute;
$second = $date - $days*24*60*60 - $hours*60*60 - $minutes*60;
$seconds = (int)$second;
$result = array($days,$hours,$minutes,$seconds);
return $result;
}

商品版php倒计时

php代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

//php的时间是以秒算。js的时间以毫秒算

date_default_timezone_set('PRC');
//date_default_timezone_set("Asia/Hong_Kong");//地区

//配置每天的活动时间段
$starttimestr = "2011-3-29 8:10:00";
$endtimestr = "2011-3-29 9:43:00";

$starttime = strtotime($starttimestr);
$endtime = strtotime($endtimestr);
$nowtime = time();
if ($nowtime<$starttime){
die("活动还没开始,活动时间是:{$starttimestr}{$endtimestr}");
}
if ($endtime>=$nowtime){
$lefttime = $endtime-$nowtime; //实际剩下的时间(秒)
 }else{
 $lefttime=0;
 die("活动已经结束!");
}
?>

JS代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<script language="JavaScript">
var runtimes = 0;
function GetRTime(){
var nMS = <?php echo $lefttime; ?>*1000-runtimes*1000;
if (nMS>=0){
var nD=Math.floor(nMS/(1000*60*60*24))%24;
var nH=Math.floor(nMS/(1000*60*60))%24;
var nM=Math.floor(nMS/(1000*60)) % 60;
var nS=Math.floor(nMS/1000) % 60;
document.getElementById("RemainD").innerHTML=nD;
document.getElementById("RemainH").innerHTML=nH;
document.getElementById("RemainM").innerHTML=nM;
document.getElementById("RemainS").innerHTML=nS;
if(nMS==5*60*1000)
{
alert("还有最后五分钟!");
}
runtimes++;
setTimeout("GetRTime()",1000);
}
}
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;
 }
}
</script>

距离活动结束还有 XXXX小时 XX分钟 XX.XX

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this post. TrackBack URL