0

I have this php:

<?php
$mytestdate = "Oct 23, 2019 20:00:00";
?>

This javascript does not give me the required output:

<script>
var mydate = "<?php echo $mytestdate; ?>";
// Set the date we're counting down to
var countDownDate = new Date(mydate).getTime();
</script>

This javascript does give me the desired output:

<script>
var mydate = "<?php echo $mytestdate; ?>";
// Set the date we're counting down to
var countDownDate = new Date("Oct 23, 2019 20:00:00").getTime();
</script>

What is going wrong?!

The output is either nothing or "NaNd NaNh NaNm NaNs".

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Neil Reardon
  • 65
  • 1
  • 9

1 Answers1

3

So when you try to run this code in a HTML file you get NaN as you are claiming,

<!DOCTYPE html>
<html>
<body>
  <?php
   $mytestdate = "Oct 23, 2019 20:00:00";
  ?>
<script>
  var mydate = "<?php echo $mytestdate; ?>";
  var countDownDate = new Date(mydate).getTime();
  alert(countDownDate);
</script>

</body>
</html>

Now, try to save this in a file with .php extension. The code will work and return you the expected result

Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20