0

I have this code that countdown 1 minute:

<span id="hour">00</span>:<span id="min">00</span>:<span id="sec">00</span>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
var deadline = new Date("<?php echo date('M d, Y H:i:s', $regaptimer); ?>").getTime(); 
var now = new Date("<?php echo date('M d, Y H:i:s', $now_time); ?>").getTime(); 
var t;
var interval=36000*2;
$(document).ready(function(){

    startCounter();
});
function startCounter()
{
    t=setInterval(function(){decrement();},1000);
}
function decrement()
{
    if(interval>0)
    {
        interval-=1000;
        formatDate();
    //  console.log("hello");
    }
    else
    {
        clearInterval(t);
    }
}
function formatDate()
{
    var remain=interval;
    var hours=parseInt(remain/3600000);
    if(hours>0)
        remain=remain-(hours*3600000);

    var minutes=parseInt(remain/60000);

    if(minutes>0)
        remain=remain-(minutes*60000);


    var seconds=parseInt(remain/1000);

//  console.log(hours,minutes,seconds);
    if(hours>9)
    $("#hour").text(hours.toString());
    else
    $("#hour").text('0'+hours.toString());
    if(minutes>9)
    $("#min").text(minutes.toString());
    else
    $("#min").text('0'+minutes.toString());
    if(seconds>9)
    $("#sec").text(seconds.toString());
    else
    $("#sec").text('0'+seconds.toString());
}</script>```

And I want to set these variables

var deadline = new Date("<?php echo date('M d, Y H:i:s', $regaptimer); ?>").getTime();
var now = new Date("<?php echo date('M d, Y H:i:s', $now_time); ?>").getTime();
var t = deadline - now;

to countdown specific time between the ending point and the starting one. I cannot figure out how to do it. Any help would be great. Many thank you in advance

Best Regards Chris

EDIT: I found this code that seems to do it but doesn't include days and hours. i need serverside 100%. I found this code seems to do the trick but missing hours and days...

<span id="countdown" class="timer"></span>
<script>
var deadline = new Date("<?php echo date('M d, Y H:i:s', $regaptimer); ?>").getTime(); 
var now = new Date("<?php echo date('M d, Y H:i:s', $now_time); ?>").getTime(); 
var seconds = deadline - now; 
function secondPassed() {
    var minutes = Math.round((seconds - 30)/60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds;  
    }
    document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
    if (seconds == 0) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "Buzz Buzz";
    } else {
        seconds--;
    }
}

var countdownTimer = setInterval('secondPassed()', 1000);
</script>

Anyone willing to help how to include them?

  • I'm pretty sure you can't just write PHP in your javascript, since PHP is server side and javascript is client side. If there's information on your server that you need in javascript, either make that info load with the page or request it through javascript. – vrugtehagel Aug 13 '19 at 22:38
  • I think the PHP is writing the script, which should work fine... but why get the date from the server-side? Seems like the client side date would be better. Maybe check this: https://stackoverflow.com/questions/9755911/send-php-date-to-javascript-date-format – pcalkins Aug 13 '19 at 23:09

0 Answers0