从mysql的特定日期倒计时

时间:2015-10-03 12:24:20

标签: javascript date countdown

这对你们来说可能很容易,但我真的不能让它发挥作用。

所以我想做的是根据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>

1 个答案:

答案 0 :(得分:1)

你可以这样做

var str = "2015-10-06 06:17:18";  // Your db format
str = str.replace(/-/g,"/"); 
var date = new Date(Date.parse(str));

这是第二行,我用/替换分隔符。这是因为Date.parse()将分隔符预期为/