PHP +js countdown function

  • 2020-03-30 03:08:22
  • OfStack

Js part


setInterval("time_controller()",1000);
function time_controller(){
    $(".time_val").each(function(){
         var time_id = "time_show_"+$(this).attr('for');
         var time_val= $(this).attr('value');
         $(this).attr('value',time_val-1);
         show_time(time_id,time_val*1000);
    });
}
function show_time(id,timestamp)
{
    var timer = document.getElementById(id);
    var str_time,int_day,int_hour,int_minute,int_second;
    var time_distance = timestamp;
    if(time_distance>0)
    {
          int_day=Math.floor(time_distance/86400000)
          time_distance-=int_day*86400000;
          int_hour=Math.floor(time_distance/3600000)
          time_distance-=int_hour*3600000;
          int_minute=Math.floor(time_distance/60000)
          time_distance-=int_minute*60000;
          int_second=Math.floor(time_distance/1000)

          if(int_hour<10)
           int_hour="0"+int_hour;
          if(int_minute<10)
           int_minute="0"+int_minute;
          if(int_second<10)
           int_second="0"+int_second;
          str_time="<b> Remaining time: "+int_day+" day "+int_hour+" hours "+int_minute+" minutes "+int_second+" seconds </b>";
          timer.innerHTML=str_time;
    }else{
          timer.innerHTML="<b> Remaining time:  0 day 0 hours 0 minutes 0 seconds </b>";
    }
}

Countdown for HTML, PHP, and js


 <div class="time" id="time_show_{$list.cmd_id}"></div>
 <input type="hidden"  class="time_val" for="{$list.cmd_id}" value="{$list.time_val}" />


Related articles: