嗯,这可能很容易为你们,但我严重不能让它工作。从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