Click here to Skip to main content
15,563,054 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new in PHP and i am having problem on my first block of code please assist me. I have a count down timer on my page, it displays correctly on Chrome and firefox but on IE and Safari it displays "NaNd NaNh NaNm NaNs " pleae help

below is my code, thank you in advance.

What I have tried:

Counter is below <!-- Counter will be inside this div form1 -->
<div id="form<?php echo $row['id'];?>" style="color:green" class="form-

function createCountDown(elementId, date) 
    // Set the date we're counting down to
    var countDownDate = new Date(date).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) 
        document.getElementById(elementId).innerHTML = "ORDER EXPIRED";
    }, 1000);
createCountDown('form<?php echo $row['id'];?>', "<?php echo $row['time_to_expire'] ;?>")
Updated 5-Jan-19 19:24pm
Mohibur Rashid 30-Aug-17 20:01pm    
try this

countDownDate = new Date("2017-08-31T08:50:00Z").getTime();
alert("countDownDate : "+countDownDate)
now = new Date().getTime();
distance = (countDownDate) - (now);

The string "NaN" stands for "Not a Number", and indicates that the value that has been calculated is out of the range of values that can exist in the system. You need to test for such values and replace them with zero. The fact that some browsers show it correctly and some do not, is down to the implementation in each browser.
Share this answer
The JavaScript Date constructor which takes a string is quite picky about the format of that string. Different browsers have varying support for different formats. Most browsers should support RFC2822 and ISO8601 date formats, but anything else is likely to cause problems.

If the format of the string you pass to the constructor is not supported, the countDownDate variable will be undefined. When you subtract the current date, you will get NaN.

You either need to make sure the date is formatted properly:
createCountDown('form<?php echo $row['id'];?>', <?php echo date_format(date_create($row['time_to_expire']), 'c') ;?>)

Or use a UNIX timestamp:
createCountDown('form<?php echo $row['id'];?>', <?php echo strtotime($row['time_to_expire']) ;?>)

Alternatively, if you know the format of the date, you can use Moment.js[^] to parse it on the client.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900