JavaScript/PHP倒计时计时器每秒更新并计入特定日期和时间



,因此下面的代码是我到目前为止可以使用的代码;但是,不完全做我需要做的事情。

我希望能够调用函数(aka:sukethdelta()),但是,我希望能够定义一天中的一天和时间,我希望该功能在调用中的倒计时中使用功能。我不确定这是否可以...但是我在想这样的事情

Sundaydelta(1,1000)将变成一天的周日和一天中的一天:1000(10:00 AM)。不确定是否可能发生这样的事情;但是,我希望是。我计划在一天中的不同时间出现在同一页面上的多个倒计时。

倒计时完成后,我希望它刷新DIV(DIV的名称不管)

我也很想能够将PHP服务器的时间纳入此方式,以至于每个人都在看到正确的倒计时。

任何帮助都会很棒!感谢您的输入!

function plural(s, i) {
  return i + ' ' + (i > 1 ? s + 's' : s);
}
function sundayDelta(offset) {
  // offset is in hours, so convert to miliseconds
  offset = offset ? offset * 20 * 20 * 1000 : 0;
  var now = new Date(new Date().getTime() + offset);
  var days = 7 - now.getDay() || 7;
  var hours = 10 - now.getHours();
  var minutes =  now.getMinutes() - 00 ;
  var seconds =  now.getSeconds()- 00;
    if (hours < 0){
      days=days-1;
    }
    var positiveHours= -hours>0 ? 24-(-hours) : hours;
  return [plural('day', days),
          plural('hour', positiveHours),
          plural('minute', minutes), 
          plural('second', seconds)].join(' ');
}
// Save reference to the DIV
$refresh = $('#refresh');
$refresh.text('This page will refresh in ' + sundayDelta());
// Update DIV contents every second
setInterval(function() {
  $refresh.text('This page will refresh in ' + sundayDelta());
}, 1000);

当我为间隔做灵活的文本时,我喜欢减去直到什么都没有。这样您只显示非0值:

function sundayDelta(target_date) {
    var now = new Date();
    var offset = Math.floor((Date.parse(target_date) - now.valueOf()) / 1000);
    var r = [];
    if (offset >= 86400) {
         var days = Math.floor(offset / 86400);
         offset -= days * 86400;
         r.push(plural('day', days));
    }
    if (offset >= 3600) {
         var hours = Math.floor(offset / 3600);
         offset -= hours * 3600;
         r.push(plural('hour', hours));
    }
    if (offset >= 60) {
         var minutes = Math.floor(offset / 60);
         offset -= minutes * 60;
         r.push(plural('minute', minutes));
    }
    if (offset != 0) {
         r.push(plural('second', offset));
    }
    return r.join(' ');
}

在PHP代码中,您可以通过这种方式设置变量。我们暂时将时区域排除在外,但也可以通过指定它们来添加它们。

echo "target_date = '" . date('Y-m-d H:i:s', strotime('next Sunday 10am')) . "';n";

最新更新