Creating a real-time clock
I need to display the date an time in the corner of my site, but to show some motio开发者_开发技巧n 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.
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.
Once you send your HTML to the client you'll never be able to change it using your sever end script without client script.
Since you already have the date printed like that, you can use Date.parse()
to get the timestamp from the string. However from my testing it seems the "at" word throws it off, so you'll need to use replace()
to remove the word "at", then put it through Date.parse()
to get the timestamp.
精彩评论