倒数计时器PHP/JavaScript [英] CountDown timer PHP/JavaScript

查看:58
本文介绍了倒数计时器PHP/JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于倒计时的JavaScript,它选择了MySQL的时间形式,并且在(Chrome,Firefox)上运行良好,但是在(IE和Safari)上返回了"NaNd NaNh NaNm NaNs".

Hi I have a JavaScript for countdown which is selecting time form MySQL and it is working fine on (Chrome, Firefox), but on (IE and Safari) it is returning "NaNd NaNh NaNm NaNs".

我在下面附加了我的代码.

I have attached my code below.

<?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>

请检查我是否再次丢失了一些东西.谢谢您的答复.

Please check if i am missing something again. thank you for all the replies.

推荐答案

编辑

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

The date passed from PHP to JavaScript Date() : 2017-09-30 00:00:00 was "malformed". It came from PHP Date("Y-m-d H:i:s"); which is VERY commonly used...

JavaScript方面的解决方法是: date = date.replace(","T");

The fix on JavaScript side is: date = date.replace(" ","T");

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

It also could be fixed on PHP side with: $date = Date("Y-m-d\TH:i:s");
Or if the date comes form a database:

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

然后,结果日期字符串为 2017-09-30T00:00:00 ,即

The resulting date string is then 2017-09-30T00:00:00, which is ISO 8601 compliant.

问题是缺少一个字符!
我会记得的.;)

这篇关于倒数计时器PHP/JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆