/    Sign up×
Community /Pin to ProfileBookmark

The sliding watch of the server

Hi,
I am looking for a php script that prints on the browser the time of the server every second. I wrote this code:

[code=php]<?php
$millisecondi=time();
echo $millisecondi;
?>[/code]

It works well but I would like to see a number that it updates every second to have a sliding watch. With javascript I have used setTimeout() but with php I did not found a such function.
Could you help me?
[B]Thanks very much[/B]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@dk_zero-coolFeb 28.2010 — PHP is server side, JavaScript is client side. You cannot get PHP to update output on the browser without updating the page.

You can use the setTimeout to reload the page every second or use AJAX to connect to the server every second, but it seams quite stupid. If you want a clock or whatever that updates, use JavaScript to build one.
Copy linkTweet thisAlerts:
@esacerbareauthorFeb 28.2010 — I built a watch in javascript with setTimeout command and it works but I would like to print on the browser the time of the server like in this example:

http://www.horlogeparlante.com/it/

I wrote two codes but it are immobile:
[code=php]<?php
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body onload="ora_di_greenwich();" style="color:#FFF; background:#000;">
<script type="text/javascript">
function ora_di_greenwich() {
';
$millisecondi=time();
echo "
localDate = new Date($millisecondi);
";
echo '
document.riferimento.greenwich.value=localDate;
setTimeout("ora_di_greenwich()",1000);
}
</script>
<form name="riferimento">
<input type="text" value="00:00:00" name="greenwich" style="width:500px; background:#E6E6E6;"/>
</form>
</body>
</html>
';
?>[/code]


[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body onload="ora_di_greenwich();" style="color:#FFF; background:#000;">
<script type="text/javascript">
function ora_di_greenwich() {
millisecondi = <?php $millisecondi=time(); echo $millisecondi; ?>;
localDate = new Date(millisecondi);
document.riferimento.greenwich.value=localDate;
setTimeout("ora_di_greenwich()",1000);
}
</script>
<form name="riferimento">
<input type="text" value="00:00:00" name="greenwich" style="width:500px; background:#E6E6E6;"/>
</form>
</body>
</html>[/code]


[B]Can I find a solution with javascript or I should use AJAX?[/B]

? ? ?
Copy linkTweet thisAlerts:
@dk_zero-coolFeb 28.2010 — The only thing you need from your PHP page, is the current time, that's it. You put that time info JavaScripts time functions and use them to create what you want. Then you loop the function every second and adds a second to the time.

One second on the browser is the same as one second on the server, so you don't need to use the server to update the time.
Copy linkTweet thisAlerts:
@esacerbareauthorMar 01.2010 — Thanks very much!

I did that you told me building a watch of Greenwich but it doesn't work. The second run but the date is wrong. I don't understand where I made a mistake! ?

[code=php]<?php
$millisecondi=time();
echo "
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body onload="ora_di_greenwich();" style="color:#FFF; background:#000;">
<script type="text/javascript">
var start = 1;
function ora_di_greenwich() {
if (start == 1) {
millisecondi = $millisecondi;
start = 2;}
else {millisecondi = millisecondi+1000;}
localDate = new Date(millisecondi);
document.riferimento.greenwich.value=localDate;
setTimeout("ora_di_greenwich()",1000);
}
</script>
<form name="riferimento">
<input type="text" value="00:00:00" name="greenwich" style="width:500px; background:#E6E6E6;"/>
</form>
</body>
</html>
";
?>[/code]
Copy linkTweet thisAlerts:
@SrWebDeveloperMar 01.2010 — [code=php]<script type="text/javascript">
<!--//

var currenttime = '<? print date("F d, Y H:i:s", time()); ?>'

///////////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("servertime").innerHTML=datestring+" "+timestring
}

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

//-->
</script>

<p><b>Current Server Time:</b> <span id="servertime"></span></p>
[/code]


This method seeds the JS date object properly plus it outputs the data in a span so it updates in real time on the page wherever you put the span.

-jim
Copy linkTweet thisAlerts:
@esacerbareauthorMar 02.2010 — [B]Thanks very much! You are a good friend and a clever programmer and also available. Thanks, thanks thanks![/B]

Your code is super but it has a little defect when the server operative system are changing the time because it must pass to DST. To understand my question you should try your code before and after DST. You can use a step of one hour so you must play with the digital watch of your operative system and xampp. I live in Rome and the DST is:

28/03/2010 - 01:59:00 -> (+ one minute) -> 28/03/2010 - 03:00:00

In London is:

28/03/2010 - 00:59:00 -> (+ one minute) -> 28/03/2010 - 02:00:00

I opened another post for this question because I think that the problem is into javascript:

http://www.webdeveloper.com/forum/showthread.php?t=225287

Nobody answered my question and I don't understand if it is too hard or people find my incorrect English too hard to understand.

Thanks for your help too!
Copy linkTweet thisAlerts:
@SrWebDeveloperMar 02.2010 — If your server time setup doesn't reflect DST automatically, then use PHP's date function with the "I" parameter to see if the current locale is in DST or not and make the adjustment. FYI, there are some interesting DST functions contributed by other users here in the comment section.
Copy linkTweet thisAlerts:
@MindzaiMar 02.2010 — Alternatively if you add the following at the start of your script:

[code=php]date_default_timezone_set('Europe/Rome');[/code]

The date functions will automatically handle this.
Copy linkTweet thisAlerts:
@esacerbareauthorMar 05.2010 — [B]Thanks very much!!!!!!!!!!!!!!!!!!!!!!!! Thanks Thanks Thanks, you are super!!![/B]

With your helps I have made progress but I have still a little problem:

[code=php]<script type="text/javascript">
/*<![CDATA[*/
serverdate = new Date(<?php echo date('Y,n-1,j,G,i,s'); ?>);
function data_del_server() {
serverdate.setSeconds(serverdate.getSeconds()+1);
currenttime = serverdate.getTime();
setTimeout("data_del_server()", 1000);
}
/*]]>*/
</script>[/code]


How can I calculate each second 'serverdate' from the server with Ajax? Could you write the solution so I try to see if I can understand you? I am studying PHP but I am still a beginner. I tried to insert a new function that winds the code of the script but it doesn't work.

[B]Thanks very much[/B]

?
×

Success!

Help @esacerbare spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...