将 Php 变量添加到 new Date()

我想将包含日期的 php 变量添加到此代码中: new Date($mydate) 如果无法使用我的 php 变量,如何将自己的日期添加到此代码中。我的日期来自 SQL 数据库,然后显示在 php 变量中。

var countDownDate = new Date("august 8, 2020 15:37:25").getTime();


千巷猫影
浏览 105回答 1
1回答

米琪卡哇伊

为了将PHP变量作为参数传递给JavaScript对象:<html><head><meta name="viewport" content="width=device-width, initial-scale=1"><style>p {&nbsp; text-align: center;&nbsp; font-size: 60px;&nbsp; margin-top: 0px;}</style></head><body><p id="demo"></p><?php $date = "07/08/2020"; ?><script>// Set the date we're counting down tovar countDownDate = new Date("<?php echo $date; ?>").getTime();// Update the count down every 1 secondvar x = setInterval(function() {&nbsp; // Get today's date and time&nbsp; var now = new Date().getTime();&nbsp; &nbsp;&nbsp;&nbsp; // Find the distance between now and the count down date&nbsp; var distance = countDownDate - now;&nbsp; &nbsp;&nbsp;&nbsp; // Time calculations for days, hours, minutes and seconds&nbsp; var days = Math.floor(distance / (1000 * 60 * 60 * 24));&nbsp; var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));&nbsp; var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));&nbsp; var seconds = Math.floor((distance % (1000 * 60)) / 1000);&nbsp; &nbsp;&nbsp;&nbsp; // Output the result in an element with id="demo"&nbsp; document.getElementById("demo").innerHTML = days + "d " + hours + "h "&nbsp; + minutes + "m " + seconds + "s ";&nbsp; &nbsp;&nbsp;&nbsp; // If the count down is over, write some text&nbsp;&nbsp; if (distance < 0) {&nbsp; &nbsp; clearInterval(x);&nbsp; &nbsp; document.getElementById("demo").innerHTML = "EXPIRED";&nbsp; }}, 1000);</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP