PHP+JS实现的商品秒杀倒计时用法示例

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP+JS实现的商品秒杀倒计时用法。分享给大家供大家参考,具体如下:

<?php
//php的时间是以秒算。js的时间以毫秒算
date_default_timezone_set('PRC');
//date_default_timezone_set("Asia/Hong_Kong");//地区
//配置每天的活动时间段
$starttimestr = "2016-3-29 8:10:00";
$endtimestr = "2016-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("活动已经结束!");
}
?>

<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>
<h4>距离活动结束还有 <strong id="RemainD">XX</strong>天 <strong id="RemainH">XX</strong>小时 <strong id="RemainM">XX</strong>分钟 <strong id="RemainS">XX</strong>.<strong id="RemainL">XX</strong>秒</h4>

PS:本站还提供了一个Unix时间戳转换工具,其中包含了PHP、JS、javascript、Python、mysql等各种常见语言的时间操作技巧,提供给大家参考:

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php smarty模板引擎的6个小技巧

下面本文将以具体的例子一一分析: capture标签 capture的中文意思是抓取,它的作用是抓取模板输出的数据,当我们需要它的时候,调用它,以得到抓取数据的目的。如下例子:复制代码...

php中3des加密代码(完全与.net中的兼容)

复制代码 代码如下: <?php class Crypt3Des { private $key = ""; private $iv = ""; /** * 构造,传递二个已经进行b...

phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别

phpinfo() 的很多部分有两个Column:Local Value和Master Value 1. Master Value是PHP.ini文件中的内容。 2.Local valu...

PHP面向对象程序设计中的self、static、parent关键字用法分析

本文实例讲述了PHP面向对象程序设计中的self、static、parent关键字用法.分享给大家供大家参考,具体如下: 看到php里面有关于后期静态绑定的内容,虽然没有完全看懂,但是也...

PHP遍历数组的几种方法

PHP中遍历数组有三种常用的方法: 一、使用for语句循环遍历数组; 二、使用foreach语句遍历数组; 三、联合使用list()、each()和while循环遍历数组。 这三种方法中...