N
N
Nikolay Firsov2015-11-16 01:51:17
PHP
Nikolay Firsov, 2015-11-16 01:51:17

How to fix now and end so that the remaining time is considered correct?

I found a ready-made timer js script and I'm trying to remake it so that the time is taken from the server, and not from the client. A very incomprehensible date format was originally used, so both end and php were transferred to UNIX format, but the difference between them is considered incorrect, please tell me how to fix this?

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title>Новый домен</title>
    <link rel="stylesheet" href="styles.css" class="css">

<?php 
$now = date('U');
$end = mktime(24, 0, 0, 12, 31, 2016);
?>

    <script>
    CountDownTimer('12/25/2015 12:00 PM', 'countdown'); //Month.Day

    function CountDownTimer(dt, id){
        var end = "<? echo $end ?>";
        var _second = 1000;
        var _minute = _second * 60;
        var _hour = _minute * 60;
        var _day = _hour * 24;
        var timer;

        function showRemaining() {
            var now = "<? echo $now ?>"
            console.log(end);
            console.log(now);
            var distance = end - now;
            if (distance < 0) {

                clearInterval(timer);
                document.getElementById(id).innerHTML = 'EXPIRED!';

                return;
            }
            var days = Math.floor(distance / _day);
            var hours = Math.floor((distance % _day) / _hour);
            var minutes = Math.floor((distance % _hour) / _minute);
            var seconds = Math.floor((distance % _minute) / _second);

            document.getElementById(id).innerHTML = days + 'days ';
            document.getElementById(id).innerHTML += hours + 'hrs ';
            document.getElementById(id).innerHTML += minutes + 'mins ';
            document.getElementById(id).innerHTML += seconds + 'secs';
        }

        timer = setInterval(showRemaining, 1000);
    }
    </script>

</head>
<body>

    <center>
      <div id="countdown"></div>
      <span id="span">remain</span>
    </center>

</body>
</html>

Initially the script looked like this
<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title>Новый домен</title>
    <link rel="stylesheet" href="styles.css" class="css">

    <center>
      <div id="countdown"></div>
      <span id="span">remain</span>
    </center>

    <script>
CountDownTimer('12/25/2015 12:00 PM', 'countdown'); //Month.Day

function CountDownTimer(dt, id)
    {
        var end = new Date(dt);
        var _second = 1000;
        var _minute = _second * 60;
        var _hour = _minute * 60;
        var _day = _hour * 24;
        var timer;

        function showRemaining() {
            var now = new Date();
            var distance = end - now;
            if (distance < 0) {

                clearInterval(timer);
                document.getElementById(id).innerHTML = 'EXPIRED!';

                return;
            }
            var days = Math.floor(distance / _day);
            var hours = Math.floor((distance % _day) / _hour);
            var minutes = Math.floor((distance % _hour) / _minute);
            var seconds = Math.floor((distance % _minute) / _second);

            document.getElementById(id).innerHTML = days + 'days ';
            document.getElementById(id).innerHTML += hours + 'hrs ';
            document.getElementById(id).innerHTML += minutes + 'mins ';
            document.getElementById(id).innerHTML += seconds + 'secs';
        }

        timer = setInterval(showRemaining, 1000);
    }
    </script>




</head>
<body>



</body>
</html>

Or tell me how to convert the date in php to the format Mon Nov 16 2015 01:59:03 GMT+0300 (RTZ 2 (winter)) I get everything except GMT and (RTZ 2). And who knows where to look for information about the value of the string CountDownTimer('12/25/2015 12:00 PM', 'countdown'); I am a complete zero in scripts, but I really want to solve the problem, I spent the whole day off)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2015-11-16
@Niolay_Firsov

but the difference between them is considered incorrect, please tell me how to fix it?

Make second equal to second.
<?php 
date("D M j Y H:i:s TP");
?>

More details - php.net/manual/ru/function.date.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question