简体   繁体   English

创建一个实时时钟

[英]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 2011年9月27日,星期二,凌晨1:34:27

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. 在几秒钟内,1:34:27将会滴答作响... 28 ... 29 ...等等

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. 您的Javascript执行并开始计算秒,分钟等,并相应地进行更改。

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. 在任何时候,您都需要参考服务器端的时间以进行日志记录或您可能需要的其他内容(通常通过表单提交,页面加载或AJAX调用来完成),使用基于服务器的时间功能,例如date('l, jS F, Y at h:i:s a') (可获取您指定的格式),并在PHP脚本的开头进行操作。 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? 因此,您基本上是在寻找一个从PHP时间读取的时钟? Have a look at this: http://jsfiddle.net/positiv/hC4yc/ - I am sure you can adapt this to do what you wan it to. 看一下这个: http : //jsfiddle.net/positiv/hC4yc/-我确信您可以修改它以完成您想要的。

将HTML发送给客户端后,如果没有客户端脚本,您将永远无法使用服务器端脚本来更改它。

Since you already have the date printed like that, you can use Date.parse() to get the timestamp from the string. 由于已经打印了日期,因此可以使用Date.parse()从字符串获取时间戳。 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. 但是从我的测试看来,“ at”一词似乎会让它丢掉,因此您需要使用replace()删除单词“ at”,然后通过Date.parse()将其Date.parse()以获得时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM