0

I have a clock that I am tring to use on my site and it works really well as in it shows the time but there are still two things that I need it to do:

  1. I need it to display the time in my timezone, the server's timezone.
  2. I need the clock to keep real time. (i.e., display the seconds as they change)

Code:

<script> 
function displayTime() { 
  document.getElementById( "servertime" ).innerHTML = '<?=date("h:i:s A");?>'; 
} 

var serverTime = window.setInterval( "displayTime()", 1200 ); 
</script> 
BryanH
  • 5,826
  • 3
  • 34
  • 47

3 Answers3

2

Set your timezone with default-timezone-set.

To move seconds you have to use JavaScript increment for Hour, minute, seconds.

Lenin
  • 570
  • 16
  • 36
2

Try something like this:

var serverTime = new Date();

function displayTime(){
    serverTime.setSeconds(serverTime.getSeconds()+1);
    document.getElementById("servertime").innerHTML = serverTime.getHours()+":"+serverTime.getMinutes()+":"+serverTime.getSeconds();
}

window.onload=function(){
    setInterval(displayTime, 1000)
}

This will use the time of the users machine. If you want to set a certain timezone, either set in your PHP configuration file or set it at beginning of your script and then put the value in the new Date javascript function. Something like:

var serverTime = new Date('<?=date("m/d/Y H:i:s T", time())?>');
fanfavorite
  • 5,128
  • 1
  • 31
  • 58
0

something like this should do it

var startTime = (function () {
  var el = document.getElementById("servertime");
function displayTime() {
  var d = new Date();
      el.innerHTML = d.getHours() + ":" + d.getMinutes() + ":"+ d.getSeconds();
}
  window.setInterval(displayTime,300)
})()

Example on JSBin

Moritz Roessler
  • 8,542
  • 26
  • 51