php+js倒计时

WBOY
Lepaskan: 2016-06-23 14:32:53
asal
1559 orang telah melayarinya

1、js的时间戳和php的时间戳转换需要注意的地方

JS时间戳为13位,包含3位毫秒的(最后面的三位),而PHP只有10位不包含毫秒的。

 

2、以下是js代码

<script type="text/javascript">var Tday = new Array();var daysms = 24 * 60 * 60 * 1000;var hoursms = 60 * 60 * 1000;var Secondms = 60 * 1000;var microsecond = 1000;var DifferHour = -1;var DifferMinute = -1;var DifferSecond = -1;function clock( key ) {    var time = new Date();    var hour = time.getHours();    var minute = time.getMinutes();    var second = time.getSeconds();    var timevalue = ""+((hour > 12) ? hour-12:hour);    timevalue +=((minute < 10) ? ":0":":")+minute;    timevalue +=((second < 10) ? ":0":":")+second;    timevalue +=((hour >12 ) ? " PM":" AM");    var convertHour = DifferHour;    var convertMinute = DifferMinute;    var convertSecond = DifferSecond;    var Diffms = Tday[key].getTime() - time.getTime();    DifferHour = Math.floor(Diffms / daysms);    Diffms -= DifferHour * daysms;    DifferMinute = Math.floor(Diffms / hoursms);    Diffms -= DifferMinute * hoursms;    DifferSecond = Math.floor(Diffms / Secondms);    Diffms -= DifferSecond * Secondms;    var dSecs = Math.floor(Diffms / microsecond);       if(convertHour != DifferHour) {        a="<font class=f4_c><strong>"+DifferHour+"</strong></font>天";    }    if(convertMinute != DifferMinute) {        b="<font class=f4_c><strong>"+DifferMinute+"</strong></font>时";    }    if(convertSecond != DifferSecond) {        c="<font class=f4_c><strong>"+DifferSecond+"</strong></font>分";    }    d="<font class=f4_c><strong>"+dSecs+"</strong></font>秒";    if (DifferHour>0) {        a=a;    } else {        a='';    }   document.getElementById("leftTime"+key).innerHTML = a + b + c + d; }</script>
Salin selepas log masuk


3、下面是模板文件

{foreach from=$goods item=goods name=goods}    <div id="leftTime{$smarty.foreach.goods.iteration}"></div>    <script type="text/javascript">        Tday[{$key}] = new Date("{$goods.gmt_end_time}");                window.setInterval(function(){clock({$key});}, 1000);         </script>{/foreach}
Salin selepas log masuk

 
Salin selepas log masuk

  

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan