To display server timer instead of showing client side timer in Javascript

0 votes
<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(){
    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);


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.


asked Jan 28, 2013 by inquirer (710 points)

Please log in or register to answer this question.