Question

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.

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.

<!DOCTYPE html>
<?php
$end_time = $_GET['time'];
$server_epoch = $_SERVER['REQUEST_TIME'];
?>
<html>
    <head>
        <script type="text/javascript">             
            var counter = setInterval(function(){
                countdown(<?= $server_epoch ?>, "<?=  $end_time?>");
                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 <= 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 ;
            }
        }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <div id="time_box">
            <form method="get" action="index.php">
                <input type="text" name="time"/>
                <input type="submit" value="submit">
            </form>
            php time: <?= $server_epoch; ?><br/>
            Current time: <div id="current"></div> <br/>
            Ends: <div id="ends"></div><br/>
            Time left: <div id="time"></div>
        </div>
    </body>
</html>
Was it helpful?

Solution 3

Turns out this is a completely awkward way to do this. This does everything I want to achieve here

OTHER TIPS

Do you ever declare the server_e variable anywhere else? It looks like this is the variable you are trying to increment, but I don't see it employed anywhere else.

I'd say a better approach would be to use http://www.datejs.com/ and set your timezone.

No use reinventing the wheel and relying on setInterval to be an accurate timer. You're going to be a second off right away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top