How to automatically update time using strftime

0

I'm putting time and date on display on my site, I'm doing this:

echo utf8_encode(strftime("%X - %m %B, %Y" ))  ;

But the time is static updating when I reload the page, I wanted to know how to make it update normally, as if it were a clock, I think it should be with javascript. Can anyone help?

    
asked by anonymous 09.03.2017 / 21:39

1 answer

1

Yes, for this you need to do with JavaScript. HTTP requests do not save states on the server (stateless), so the PHP script stops executing after returning the response to the request. This will only display the time (from the server) that was run the code.

JavaScript

Solution in JavaScript Retrieval from here :

function checkTime(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

function startTime() {
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  // add a zero in front of numbers<10
  m = checkTime(m);
  s = checkTime(s);
  document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
  t = setTimeout(function() {
    startTime()
  }, 500);
}
startTime();
<div id="time"></div>

This is a function that retrieves the hour, minute, and second values using the getHours , getMinutes , and getSeconds methods of the Date object. After, it formats minutes and seconds to display "01" instead of just "1", for example. The setTimeout JavaScript function is also used to keep the time count running.

    
09.03.2017 / 21:48