I try to display the current time in my server(the web page it’s in my server) but when I open the web page I get the time on the computer that runs the page.
Is there a way to do that ?
This is my code :
function startTime(){
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m=checkTime(m);
s=checkTime(s);
$('#cTime').val(h+":"+m+":"+s);
t=setTimeout(function(){startTime()},500);
}
function checkTime(i){
if (i<10) {
i="0" + i;
}
return i;
}
Thank you.
JavaScript will always return the time on the computer it’s run on since it’s executed client-side.
You can write a simple script on your server that outputs the current time and send a request to that script via JavaScript.
You can do something like this:
PHP
JavaScript