<div id='countdown'></div>
<button onclick='startCountdown()'>Start Countdown</button>
<script>
function startCountdown() {
  var remainingTime = <?php echo $remaining_time; ?>;
  var days = Math.floor(remainingTime / (24 * 60 * 60));
  var hours = Math.floor((remainingTime % (24 * 60 * 60)) / (60 * 60));
  var minutes = Math.floor((remainingTime % (60 * 60)) / 60);
  var seconds = Math.floor(remainingTime % 60);

  var countdown = document.getElementById('countdown');
  countdown.innerHTML = 'Remaining Time: ' + days + 'd ' + hours + 'h ' + minutes + 'm ' + seconds + 's';

  var countdownInterval = setInterval(function() {
    remainingTime--;
    days = Math.floor(remainingTime / (24 * 60 * 60));
    hours = Math.floor((remainingTime % (24 * 60 * 60)) / (60 * 60));
    minutes = Math.floor((remainingTime % (60 * 60)) / 60);
    seconds = Math.floor(remainingTime % 60);

    countdown.innerHTML = 'Remaining Time: ' + days + 'd ' + hours + 'h ' + minutes + 'm ' + seconds + 's';

    if (remainingTime <= 0) {
      clearInterval(countdownInterval);
      countdown.innerHTML = 'Expired';
    }
  }, 1000);
}
</script>
PHP 和 JavaScript 倒计时:计算剩余时间并显示在 HTML 页面

原文地址: https://www.cveoy.top/t/topic/muGr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录