简体   繁体   English

如何递减给定的时间

[英]How to decrement given time

I am getting an output of 00:20:00 which is correct but my problem now is its not decrementing even when I have subtracted it am i missing something?我得到 00:20:00 的输出,这是正确的,但我现在的问题是即使我减去它也不会递减它是我遗漏了什么?

$duration=0;
$startime=date('Y-m-d H:i:s');

$end_time=$end_time=date('Y-m-d H:i:s', 
strtotime('+'.$duration.'minutes',strtotime($startime)));

$timefirst=strtotime($startime);
$timesecond=strtotime($end_time);
$differenceinseconds=$timesecond-$timefirst;
echo gmdate("H:i:s", $differenceinseconds);

my script我的剧本

 <div id='response'></div>

 <script type="text/javascript">
 setInterval(function(){
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET",'responseTime.php',false);
    xmlhttp.send(null);
    document.getElementById('response').innerHTML=xmlhttp.responseText;
 },1000);
     </script>

As RiggsFolly mentioned why waste servers time running a timer.正如 RiggsFolly 提到的,为什么要浪费服务器时间来运行计时器。 Here is what you can do in javascript,这是您可以在 javascript 中执行的操作,

<div id="stopwatch"></div>
<script>
var Stopwatch = function (elem, target, options) {

var timer = createTimer(),
        offset,
        clock,
        interval;

// default options
options = options || {};
options.delay = options.delay || 1;

// append elements     
elem.appendChild(timer);

// initialize
reset();

// private functions
function createTimer() {
    var interval = 20 ; // 20 seconds
    var element = document.createElement("progress");
    element.setAttribute("max",interval);
    return element;
}

function start() {
    if (!interval) {
        offset = Date.now();
        interval = setInterval(update, options.delay);
    }
}

function stop() {
    if (interval) {
        clearInterval(interval);
        interval = null;
    }
}

function reset() {
    clock = 0;
    render();
}

function update() {
    clock += delta();
    render();
}

function render() {
    timer.value = parseInt(clock / 1000);
    if(timer.value==interval){
        // This is the point where timer ends, put your further code in here.
    }
}

function delta() {
    var now = Date.now(),
            d = now - offset;

    offset = now;
    return d;
}

// public API
this.start = start;
this.stop = stop;
this.reset = reset;
};
var elem = document.getElementById("stopwatch");
var timer = new Stopwatch(elem, {delay: 10});
timer.start();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM