2017-02-24 90 views
0

我试图在我的网站上获得一个倒计时系统,从注册时间开始只有24小时倒计时。PHP中的日期差异

这个想法是注册后,订户有24小时付款后,他将被阻止。我的注册时间已经在我的数据库中,但我陷入了倒计时。我在网上尝试了几种帮助,但我似乎没有把它做对。

JavaScript设置为用户付款的剩余秒数var count = '86400';但我想使用php来计算从注册时间到当前时间他已注册多长时间,以便当我刷新该页面,它不会从当前剩余时间开始。所以我做了这个var count = <?php echo strtotime($row_rsPH['date'])-time(); ?>;

   <script type="text/javascript"> 
      var count = <?php echo strtotime($row_rsPH['date'])-time(); ?>; 
      var counter = setInterval(timer, 1000); 

      function timer() { 
       count = count - 1; 
       if (count == -1) { 
        clearInterval(counter); 
        return; 
       } 

       var seconds = count % 60; 
       var minutes = Math.floor(count/60); 
       var hours = Math.floor(minutes/60); 
       minutes %= 60; 
       hours %= 60; 

       document.getElementById("timer").innerHTML = hours + ":" + minutes + ":" + seconds; // watch for spelling 
      } 




      function startTimer(duration, display) { 
       var start = Date.now(), 
        diff, 
        minutes, 
        seconds; 
       function timer() { 
        // get the number of seconds that have elapsed since 
        // startTimer() was called 
        diff = duration - (((Date.now() - start)/1000) | 0); 

        // does the same job as parseInt truncates the float 
        minutes = (diff/60) | 0; 
        seconds = (diff % 60) | 0; 

        minutes = minutes < 10 ? "0" + minutes : minutes; 
        seconds = seconds < 10 ? "0" + seconds : seconds; 

        display.textContent = minutes + ":" + seconds; 

        if (diff <= 0) { 
         // add one second so that the count down starts at the full duration 
         // example 05:00 not 04:59 
         start = Date.now() + 1000; 
        } 
       }; 
       // we don't want to wait a full second before the timer starts 
       timer(); 
       setInterval(timer, 1000); 
      } 

      </script> 
      <p>Time Left to make payment: <span id='timer'></span></p> 
+0

http://php.net/ manual/en/datetime.sub.php – Sammitch

+0

你有这个任何PHP?它被标记为这样的 –

+0

嗨@ Fred-ii-是的,我做'<?php回声strtotime($ row_rsPH ['date']) - time(); ?>' – Jahswey

回答

0

我用了一个倒计时样品这里http://www.hashemian.com/tools/javascript-countdown.htm和TargetDate下,我用这个PHP代码

<?php 
$date = date_create($row_rsPH['date']); 
date_add($date, date_interval_create_from_date_string('1 days')); 
echo date_format($date, 'Y-m-d h:i A'); 
?> 

希望这有助于别人

+0

您可以选择自己的答案为正确的,如果这是您解决问题的方式。 –