嗯,這可能很容易爲你們,但我嚴重不能讓它工作。從mysql的特定日期倒計時
所以我想要做的是根據mysql的日期進行倒計時,並使其在實時模式下無需刷新。
代碼:
<?php
$date = strtotime($row_tournaments['date']);
$remaining = $date - time();
$days_remaining = floor($remaining/86400);
$hours_remaining = floor(($remaining % 86400)/3600);
$minutes_remaining = floor(($remaining % 3600)/60);
$seconds_remaining = ($remaining % 60);
echo "<p>$days_remaining <span style='font-size:.3em;'>dias</span> $hours_remaining <span style='font-size:.3em;'>horas</span> $minutes_remaining <span style='font-size:.3em;'>minutos</span> $seconds_remaining <span style='font-size:.3em;'>segundos</span></p>";
?>
這工作得很好,但我需要刷新,所以我能看到的時間下降。
所以我覺得這個答案,
代碼:
<p id="counter"></p>
<script>
var counter = new Date(2015,10,10,20,25,30);
var counterElem = document.getElementById("counter");
setInterval(function()
{
counter.setSeconds(counter.getSeconds() - 1);
counterElem.innerHTML = counter.getDate()+" <span style=\'font-size:.3em;\'>dias</span> "+counter.getHours()+" <span style=\'font-size:.3em;\'>horas</span> "+counter.getMinutes()+" <span style=\'font-size:.3em;\'>minutos</span> "+counter.getSeconds()+" <span style=\'font-size:.3em;\'>segundos</span>";
},1000);
</script>
的實際工作相當不錯,但問題是,
new Date(2015,10,10,20,25,30)
只接受這種格式和我的數據庫中日期格式爲
2015-10-06 06:17:18
而我不知道如何轉換。
請大家幫忙,如果你們有更好的解決方案,非常歡迎。
謝謝你的時間。
Cumps。
所有代碼:
<div id="countdownContainer">
<a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>"><img src="images/wallpapers/<?php echo $row_tournaments['logo']; ?>.jpg"></a>
<div id="countdownContent">
<p>Próximo Torneio:</p>
<?php
$date = strtotime($row_tournaments['date']);
$remaining = $date - time();
$days_remaining = floor($remaining/86400);
$hours_remaining = floor(($remaining % 86400)/3600);
$minutes_remaining = floor(($remaining % 3600)/60);
$seconds_remaining = ($remaining % 60);
echo "<p>$days_remaining <span style='font-size:.3em;'>dias</span> $hours_remaining <span style='font-size:.3em;'>horas</span> $minutes_remaining <span style='font-size:.3em;'>minutos</span> $seconds_remaining <span style='font-size:.3em;'>segundos</span></p>";
?>
<p id="counter"></p>
<script>
str = "$row_tournaments['date']"; // Your db format var date = new Date(Date.parse(str));
str = str.replace(/-/g,"/");
var counter = new Date(Date.parse(str));
var counterElem = document.getElementById("counter");
setInterval(function()
{
counter.setSeconds(counter.getSeconds() - 1);
counterElem.innerHTML = counter.getDate()+" <span style=\'font-size:.3em;\'>dias</span> "+counter.getHours()+" <span style=\'font-size:.3em;\'>horas</span> "+counter.getMinutes()+" <span style=\'font-size:.3em;\'>minutos</span> "+counter.getSeconds()+" <span style=\'font-size:.3em;\'>segundos</span>";
},1000);
</script>
</div>
</div>
將您的字符串從數據庫轉換爲'var date = new Date(Date.parse(「2015-10-06 06:17:18」));' – Saar
以及我可以工作,但可能會工作,但問題我做錯了,我應該把這樣的代碼var counter = new Date(Date.parse($ row_tournaments ['date']));?因爲如果是的話,它不起作用的日期不顯示。 – Bruno
@Saar在某些瀏覽器中返回無效日期,因爲字符串無效格式 – charlietfl