Reputation: 57
<div id="clock"></div>
<script>
var now = new Date(<?php echo time() * 1000 ?>);
function startInterval(){
setInterval('updateTime();', 1000);
}
startInterval();//start it right away
function updateTime(){
var nowMS = now.getTime();
nowMS += 1000;
now.setTime(nowMS);
var clock = document.getElementById('clock');
if(clock){
clock.innerHTML = now.toTimeString();//adjust to suit
}
}
This is my html code. I am a Java noob and basically found this here: PHP with javascript code, live clock. I want to adjust the format from "HH:mm:ss GMT+0100 (Romansk (normaltid))" to only "HH:mm:ss".
Upvotes: 0
Views: 170
Reputation: 22471
Try Moment.js:
clock.innerHTML = moment(now).format('HH:mm:ss');
BTW, you are using PHP time()
to return server side time right? Just a hint, setInterval
is not that accurate (see: How Accurate is Window.setInterval()) and so you will slowly introduce imprecision. If client side clock can't be trusted I would either expose the server time as Web Service or consume the time from one of the many web services already available (Time API, json-time, etc). While there will be a delay, at least it will not introduce cumulative imprecision.
Upvotes: 1
Reputation: 7428
now.toLocaleTimeString(false, {'hour12': false});
or instead of false
- your prefered locale
Upvotes: 0