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.
EDIT
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...
The fix on JavaScript side is: date = date.replace(" ","T");
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;?>")
The resulting date string is then 2017-09-30T00:00:00
, which is ISO 8601 compliant.
The issue was abvout ONE character!
I'll remember that one.
;)
Check carefully '
single quotes here, Use double quotes for first parameter as second param.
createCountDown("form<?php echo $row['id'];?>", "<?php echo $row['time_to_expire'] ;?>")
Here is working example
function createCountDown(elementId, date)
{
// Set the date we're counting down to
var countDownDate = new Date(date).getTime();
//console.log(countDownDate.getTime());
// 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));
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('form1', "08-09-2017 12:10:00Z");
createCountDown('form2', "08-09-2018 12:10:00Z");
<div id="form1"></div>
<div id="form2"></div>
Use moment.js and try below code for timer
var eventTime = moment("target time").unix(),
currentTime = moment().unix(),
diffTime = eventTime - currentTime,
duration = moment.duration(diffTime * 1000, "milliseconds"),
interval = 1000;
if (diffTime > 0) {
var timer = setInterval(function () {
duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
var h = moment.duration(duration).hours(),
m = moment.duration(duration).minutes(),
s = moment.duration(duration).seconds();
// show how many hours, minutes and seconds are left
var temp = '<div><span>' + h + '</span> hr </div> <div><span>'
+ m + '</span> min </div> <div><span>' + s +
'</span> sec </div>';
$(elementId).html(temp);
if ((h == 0 && m == 0 && s == 0) || (s < 0)) {
clearInterval(timer);
}, interval);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With