PHP / Javascript在2次之间创建倒计时

时间:2015-05-04 13:29:51

标签: javascript php

我目前在尝试思考如何创建此脚本时遇到很多问题。

我目前有两个变量:

$curHour = "15:25:00";
$endHour = "16:25:00";

我正在努力寻找如何在这些时间之间特定地创建倒计时。

我将不胜感激。

问候格伦

2 个答案:

答案 0 :(得分:0)

您可以尝试如下,

setInterval(function () {

    var end = <?php  echo strtotime('16:25:00')*1000;?>;//Javascriptcomapatible time
    var current = new Date().getTime(); //Current timestamp
    var seconds_left = (end - current) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    console.log(days + "d, " + hours + "h,"+ minutes + "m, " + seconds + "s");  

}, 1000);

答案 1 :(得分:0)

我刚刚完成了javascript,但代码似乎有点长。
HTML:

<div id="process"></div>

的javascript:

var process = document.getElementById('process');

var cur = '15:25:00';
var end = '16:25:00';

cur = cur.split(':');
end = end.split(':');
var x = parseInt(cur[0], 10)*3600+parseInt(cur[1], 10)*60+parseInt(cur[2], 10);
var y = parseInt(end[0], 10)*3600+parseInt(end[1], 10)*60+parseInt(end[2], 10);

showTime(y);
var timer = setInterval(function(){
    y--;
    showTime(y);
    if(y<=x){
        clearInterval(timer);
    }
}, 1000);

function showTime(diff){
    var hour = parseInt(diff/3600, 10);
    var minute = parseInt((diff - hour*3600)/60, 10);
    var second = diff - hour*3600-minute*60;

    hour   = hour<10 ? '0'+hour : hour;
    minute = minute<10 ? '0'+minute : minute;
    second = second<10 ? '0'+second : second;

    var time = hour+':'+minute+':'+second;
    process.innerHTML = time;
}