gpt4 book ai didi

javascript - 倒数计时器 PHP/JavaScript

转载 作者:可可西里 更新时间:2023-11-01 12:53:59 25 4
gpt4 key购买 nike

enter image description here您好,我有一个用于倒计时的 JavaScript,它从 MySQL 中选择时间,它在(Chrome、Firefox)上运行良好,但在(IE 和 Safari)上它返回“NaNd NaNh NaNm NaNs”。

我在下面附上了我的代码。

<?php
$con = mysqli_connect("localhost", "root", "", "timer") or die("Error Could
not connect to the database Sir." . mysqli_error($con));

$query = mysqli_query($con, "SELECT * FROM counter WHERE id = 1") or
die(mysqli_error($con));
$row = mysqli_fetch_array($query)or die(mysqli_error($con));



?>
<div id="form<?php echo $row['id'];?>" style="color:green" class="form-
group">
</div>


<Script>
function createCountDown(elementId, date){
console.log(date);
// Set the date we're counting down to
var countDownDate = new Date(date).getTime();
console.log(countDownDate);

// Update the count down every 1 second
var x = setInterval(function(){

// Get todays date and time


var now = new Date().getTime();

// Find the distance between now an the count down date
var distance = (countDownDate) - (now);

//Hint on converting from object to the string.
//var distance = Date.parse(countDownDate) - Date.parse(now);

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
//console.log(days);
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 *
60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Display the result in the element with id="demo"
document.getElementById(elementId).innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is finished, write some text
if (distance < 0)
{
clearInterval(x);
document.getElementById(elementId).innerHTML = "ORDER EXPIRED";
}
}, 1000);
}
createCountDown("form<?php echo $row['id'];?>", "<?php echo
$row['time_to_expire'] ;?>")
</Script>

请检查我是否又遗漏了什么。谢谢大家的回复。

最佳答案

编辑

Internet Explorer 和 Safari 不能容忍格式错误的日期。

从 PHP 传递到 JavaScript 的日期 Date() : 2017-09-30 00:00:00“格式错误”。它来自非常常用的 PHP Date("Y-m-d H:i:s");...

JavaScript 方面的修复是:date = date.replace("","T");

它也可以在 PHP 端修复:$date = Date("Y-m-d\TH:i:s");
或者如果日期来自数据库:

$date = str_replace(" ","T",$row['time_to_expire']);
createCountDown("form<?php echo $row['id'];?>", "<?php $date;?>")

生成的日期字符串为 2017-09-30T00:00:00,即 ISO 8601合规。

问题是一个字符!
我会记住那个。;)

关于javascript - 倒数计时器 PHP/JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46109832/

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