gpt4 book ai didi

javascript - 从mysql的特定日期开始倒计时

转载 作者:行者123 更新时间:2023-12-03 08:45:16 26 4
gpt4 key购买 nike

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

所以我想做的是根据 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 str = "2015-10-06 06:17:18";  // Your db format
str = str.replace(/-/g,"/");
var date = new Date(Date.parse(str));

在第二行中,我将分隔符 - 替换为/。这是因为 Date.parse() 期望分隔符为/

关于javascript - 从mysql的特定日期开始倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32922518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com