<div class="grid--cell fl1 lh-lg">
<div class="grid--cell fl1 lh-lg">
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, <a href="/help/reopen-questions">visit the help center</a> for guidance.
</div>
</div>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2012-05-30 17:12:09Z" class="relativetime">7 years ago</span>.</div>
</div>
</aside>
I want to show server time on my web page. I'd done client time with javascript codes but how I can show server time?
is any zendFramework classes do this?
I'm using below javascript code to show client time:
<head>
<script language="javascript" type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById("time_section").innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
</body>
</div>
to show the server time use the date function:
<?php echo date('Y-m-d H:i:s'); ?>
if you mean a realtime clock you can't do that with php, stick with javascript for that
Ask the server for the time by AJAX, as UNIX timestamp (PHP function time()
). When it arrives, get the difference from the local timestamp (var timeDiff = new Date() - serverTime * 1000
); modify your not-quite-irrelevant code to show new Date() + timeDiff
instead of new Date()
.
Let's try some code:
var timeDiff;
$.ajax('time.php', {
success: function(serverTime) {
timeDiff = new Date() - serverTime * 1000;
startTime();
}
});
replace newDate()
with newDate() + timeDiff
in your startTime
. On the serverside, it's simple, in time.php
:
header('Content-Type: text-json');
echo time();
Didn't check, so there might be bugs. Caveat emptor.
h3nr1x's solution would also work, but this way you won't have to worry about going out of sync.
A small glitch is that the time calculation on the clientside is all being done in the local time zone; this will affect you if you cross the DST time at the different time than the server, and in the meantime the time shown would be incorrect. I do not know the way to set the time zone of the JavaScript's Date
object, or if it is even possible. :(
Set server timezone
date_default_timezone_set('America/Los_Angeles');
Will display 17:16:18
$currentTime = date("H:i:s");
see date_default_timezone_set() and date() for confiruation options.
Modify your code so, the setTimeout function only increments the clock in the client every minute, then initialize the today
javascript variable with the server time using php, like this:
// time() returns seconds, multiply by 1000 to get millisenconds
var today = new Date(<?php echo time() * 1000?>);
Then the javascript clock should keep going with the server time in the client
Optionally, you can synchronize with the server time by doing an ajax request every certain amount of time (p.e an hour)
You can do it with AJAX. Have a php page which echo the current time and update it every second or whatever with the ajax.