Displaying timer from server time instead of using client side time in Javascript
Sunday, June 20, 2010 14:01Posted in category Javascript
Here is the useful Javascript function, which retrieves the Server Date Time from PHP and stores into Javascript variable. Finally runs the timer in Javascript.
<script type="text/javascript">
var currenttime = '<? echo date("F d, Y H:i:s", time())?>' //PHP method of getting server date
///////////Stop editting here/////////////////////////////////
var montharray= new Array("January","February","March","April","May","June", "July","August","September","October","November","December");
var serverdate=new Date(currenttime);
function padlength(what){
var output=(what.toString().length==1)? "0"+what : what;
return output;
}
function displaytime(){
serverdate.setSeconds(serverdate.getSeconds()+1);
var datestring=montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear();
var timestring = padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+ ":" + padlength(serverdate.getSeconds());
document.getElementById("clock").innerHTML = datestring + " " + timestring;
}
setInterval("displaytime()", 1000);
</script>
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Felipe Whittman says:
July 24th, 2011 at 11:37 am
I like reading websites like these. It has been an nice source of info. I could not locate sites like these. Thanks once again.