0
var countDownDate = new Date("<?php echo file_get_contents('wipetime.txt');?>").getTime();

I'm trying to make a countdown timer. The file "wipetime.txt" changes every few days so I'm trying to make it so I can grab the file when the page loads and countdown to that date.

The problem is that if I echo that php value somewhere on the page, it works. It displays the file contents. However, if I were to echo it inside countDownDate or even an alert, I get nothing. No error, just completely nothing.

I can't see anything wrong at all.

Edit: Heres the entire thing in case you wanna know

<!--Whitelist timer-->
<script>
var countDownDate = new Date("<?php echo file_get_contents('wipetime.txt');?>").getTime();
var x = setInterval(function() {
  var now = new Date().getTime();
  var distance = countDownDate - now;
  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);
  document.getElementById("countdown").innerHTML = "Whitelist expires in " + days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("countdown").innerHTML = "EXPIRED";
  }
}, 1000);
</script>
7
  • Can you show the generated html? Show us what appears under page-source/view-source. Also is this a .js file? Commented Sep 30, 2018 at 22:38
  • And I'm sorry if the info i've given is kind of vague. I don't really know what else to say, this is really basic and /should/ work, but for some reason I just get nothing. Am I overlooking something stupid? Commented Sep 30, 2018 at 22:39
  • The problem is that we can't tell what is the generated code on the page. We can't tell if you are doing this in a php file or a js file. Commented Sep 30, 2018 at 22:42
  • var countDownDate = new Date("Oct 05, 2018 06:39:03 ").getTime(); Thats what chrome console is saying it outputted. The entire page is in php but the specific javascript is at the end of the page Commented Sep 30, 2018 at 22:43
  • If I paste Oct 05, 2018 06:39:03 into the quotes rather than the php, it works, but that means I can't get the date dynamically, which is what I'm trying to do. If I use the script I posted above, it doesn't display anything at all Commented Sep 30, 2018 at 22:45

1 Answer 1

3

There is a possibility that the file wipetime.txt contains some special characters (maybe a carriage return). Two things you can do:

  1. Manually check the file for any artifacts and remove them.
  2. You should trim the content.

The code:

<?php
$content = file_get_contents('wipetime.txt');
$timeStr = trim($content); // should remove trailing characters like new lines. 
?>

var countDownDate = new Date("<?php echo $timeStr;?>").getTime();
Sign up to request clarification or add additional context in comments.

2 Comments

I'm generating wipetime.txt with the command line in linux and for some reason its outputting an extra line, so this answer works perfectly.
They're going to be out of luck if the value contains a " for some reason. json_encode() is a handy way to escape things for insertion into JavaScript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.