PHP时间戳实现倒计时,JavaScript | 时间戳实现倒计时定时器
时间: 2024-03-17 08:19:48 浏览: 64
PHP时间戳实现倒计时:
```php
$future_date = strtotime('2022-01-01 00:00:00');
$current_date = time();
$diff = $future_date - $current_date;
echo '倒计时: ' . floor($diff / (60 * 60 * 24)) . '天 ' . gmdate('H:i:s', $diff % (60 * 60 * 24));
```
JavaScript时间戳实现倒计时定时器:
```html
<span id="countdown"></span>
<script>
var countdownElement = document.getElementById('countdown');
var futureDate = new Date('2022-01-01 00:00:00').getTime();
var countdownInterval = setInterval(function() {
var currentDate = new Date().getTime();
var diff = futureDate - currentDate;
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
var hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((diff % (1000 * 60)) / 1000);
countdownElement.innerHTML = '倒计时: ' + days + '天 ' + hours + '小时 ' + minutes + '分 ' + seconds + '秒 ';
if (diff < 0) {
clearInterval(countdownInterval);
countdownElement.innerHTML = '时间到了!';
}
}, 1000);
</script>
```
阅读全文