This topic has been closed for further discussion.
Administrator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 3166
I've been thanked 101 times.
Offline
I've seen the time displayed on the page in the form of real-time clocks that use javascript
As far as I am aware, because this is client-side, it uses the time on the user's local machine.
When a page loads I want to 'seed' a javascript clock with the server time (given by PHP), and then have the js clock contine to show the server time, and update with each second.
I really don't know javascript that well, so some help will be needed
Administrator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 3166
I've been thanked 101 times.
Offline
Found something, hope it will help someone:
Code: javascript
<script type="text/javascript"> var currenttime = '<? print date("F d, Y H:i:s", time())?>' //PHP method of getting server date 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>
That's pretty neat!
Code: php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <title>Javascript and PHP time</title> </head> <body> <script type="text/javascript"> <!-- var currenttime = '<?php print date("F d, Y H:i:s", mktime(23, 59, 50, 12, 31, 1975))?>' 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> <h1>Javascript and PHP time</h1> <p><b>Current Server Time:</b></p> <span id="servertime"><?php print date("F d, Y H:i:s", mktime(23, 59, 50, 12, 31, 1975))?></span> </body> </html>
Thanks to mktime rather than time you can set any date in the past or future and the javascript will keep up with it. Check out the example to see how it crosses years.
The only difference other than the mktime is that I'm filling the span with the php date to avoid the awkward screen flash on load.
I'm sure that'll come in handy sometime, thanks!
Administrator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 3166
I've been thanked 101 times.
Offline
I thought it might be handy for staff members using the admin panel - I use it to display 'time to next....'
I've just built an auto responder with mail queue that integrates with our 30+ databases. Several crons are in place, one runs every two hours and populates the queue, (which is examined every minute). If a mistake is made one can halt the queue, hence there is time to review what's been done. Good practice is to leave enough time to undo mistakes, and for that you need to know what time everything is happening
| Never |


