Update number every hour with setInterval [closed]


For now I have only been able to do this code

var time = 1;
var myInterval = setInterval(funtion(){
    time += 1;

I need a div, each time the number is increased after one hour,

<div id="time">1</div>

The problem is also that this number is saved in cookies, or in a small sql, something, so that no matter who reload the page, follow from the number left.

Thank you very much!

asked by Juan David 13.02.2017 в 19:16

1 answer


You could make a solution without using Javascript or cookies in the following way:

  • Create a table with a column named "date" of type DATETIME
  • Insert the current date and time in that column INSERT INTO fechas (fecha) VALUES (now());
  • Each time the page is loaded the difference SELECT time_format(timediff(now(),fecha),'%H') FROM pruebafecha.fechas LIMIT 1;
  • Making use of the wordpress variable $ wpdb would be:

        global $wpdb;
        $horas = $wpdb->get_var("SELECT time_format(timediff(now(),fecha),'%H') FROM pruebafecha.fechas LIMIT 1");
    <div id="time"><?php echo $horas ?></div>

    I hope I helped you!


    answered by 13.02.2017 / 19:37