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.