2017-02-24 153 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

您可以選擇自己的答案爲正確的,如果這是您解決問題的方式。 –