【发布时间】:2015-10-03 12:24:19
【问题描述】:
嗯,这对你们来说可能很容易,但我真的无法让它发挥作用。
所以我想做的是根据 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
我不知道如何转换。
请帮忙,如果你们有更好的解决方案,欢迎。
感谢您的宝贵时间。
肿块。
所有代码:
<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")); -
好吧,我猜这可以工作,但我可能做错了什么,我应该把这样的代码 var counter = new Date(Date.parse($row_tournaments['date']));?因为如果是,它不起作用,日期不会显示。
-
@Saar 在某些浏览器中返回无效日期,因为字符串格式无效
-
任何让它工作的技巧,我真的想不出任何东西。肿块。
-
为我工作。除非你提供了错误的信息
标签: javascript date countdown