Search code examples
phpdatetimereal-timeclock

Creating a real-time clock


I need to display the date an time in the corner of my site, but to show some motion on the page. The formatting is simple.

Tuesday, September 27, 2011 at 1:34:27 am

However, I'd like the entire thing to change in real time. So after a day has passed if the visitor is still not he site, Tuesday will say Wednesday. And in seconds, 1:34:27 will be ticking along... 28... 29... etc.

Pretty simple, right? It's just text. Can't seem to figure this out.


Solution

  • So when your user lands on the page, you feed server time and date in whatever format you require to your page.

    Your Javascript executes and starts counting the seconds, minutes etc and changes accordingly.

    At any point you need to make a reference to time on the server end for logging or what ever else you may require (this will usually be done through a form submit, page load or AJAX call), use the server based time functions, like date('l, jS F, Y at h:i:s a') (which gets you the format you specified) at the start of the PHP script and manipulate as you want. You then have the timestamp when the user performed that particular action.

    edit...

    So you are basically looking for a clock that reads from PHP time? Have a look at this: http://jsfiddle.net/positiv/hC4yc/ - I am sure you can adapt this to do what you wan it to.