title: Javascript countdown timer using epoch server time
body: <p>I'm trying to make a count down timer that counts down from current time to a set time in the future. I got it working perfectly using just JS but I need to use the server's current time not the clients.</p>
  
  <p>I've attempted this by using PHP to get the server epoch time, then I want to increment it every second and convert into a Javascript Date object so I can do comparisons and write the remaining time to the DOM. I don't know if this is possible or even the best way to do it but at the moment its wont increment or give a valid/correct date from the server.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;?php
  $end_time = $_GET['time'];
  $server_epoch = $_SERVER['REQUEST_TIME'];
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript"&gt;             
              var counter = setInterval(function(){
                  countdown(&lt;?= $server_epoch ?&gt;, "&lt;?=  $end_time?&gt;");
                  server_e++;
              }, 1000);
  
  
  
          /*
           * Calculates the time remaining to the nearest second from the current time until the
           * end time and updates DOM elements with id of "time" to display the formatted time(D,H,M,S)
           */
          function countdown(server, end_time){
              var current_time = new Date(server);
              var end = new Date(end_time);
              var time_left = (end - current_time) / 1000;
              if(time_left &lt;= 0){
                  document.getElementById("time").innerHTML= "Auction has ended" ;
              } else {
                  var days = Math.floor(time_left / 86400);
                  var hours = Math.floor(((time_left / 86400)-days) * 24);
                  var mins = Math.floor(((((time_left / 86400)-days) * 24)-hours)* 60);
                  var secs = Math.floor(((((((time_left / 86400)-days) * 24)-hours)* 60)-mins) * 60);
                  var day_string = " days, ";
                  if (days == 1){
                      day_string = " day, "                
                  }      
                  document.getElementById("time").innerHTML= days + day_string + hours +" hours, " + mins +" mins, "+ secs+" secs" ;  
                  document.getElementById("current").innerHTML= current_time ;
                  document.getElementById("ends").innerHTML= end ;
              }
          }
          &lt;/script&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div id="time_box"&gt;
              &lt;form method="get" action="index.php"&gt;
                  &lt;input type="text" name="time"/&gt;
                  &lt;input type="submit" value="submit"&gt;
              &lt;/form&gt;
              php time: &lt;?= $server_epoch; ?&gt;&lt;br/&gt;
              Current time: &lt;div id="current"&gt;&lt;/div&gt; &lt;br/&gt;
              Ends: &lt;div id="ends"&gt;&lt;/div&gt;&lt;br/&gt;
              Time left: &lt;div id="time"&gt;&lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>