创建实时时钟
我需要在网站的一角显示日期和时间,但要在页面上显示一些动作。格式化很简单。
2011 年 9 月 27 日星期二上午 1:34:27
然而,我希望整个事情能够实时改变。因此,一天过去后,如果访问者仍未到达现场,则星期二将显示星期三。几秒钟后,1:34:27 就会滴答作响... 28... 29... 等等。
很简单,对吧?这只是文字。似乎无法弄清楚这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,当您的用户登陆页面时,您可以按照页面所需的任何格式向服务器提供时间和日期。
你的 JavaScript 执行并开始计算秒、分钟等并相应地改变。
在任何时候,您需要在服务器端引用时间以进行日志记录或您可能需要的任何其他内容(这通常通过表单提交、页面加载或 AJAX 调用来完成),请使用基于服务器的时间函数,例如
date('l, jS F, Y at h:i:s a')
(它会获取您指定的格式)在 PHP 脚本的开头并根据需要进行操作。然后,您将获得用户执行该特定操作时的时间戳。编辑...
所以您基本上是在寻找一个读取 PHP 时间的时钟?
看看这个: http://jsfiddle.net/positiv/hC4yc/ - 我确信您可以对其进行调整以执行您想要的操作。
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.
一旦您将 HTML 发送到客户端,您将永远无法在没有客户端脚本的情况下使用服务器端脚本更改它。
Once you send your HTML to the client you'll never be able to change it using your sever end script without client script.
由于您已经像这样打印了日期,因此您可以使用 Date.parse() 从字符串中获取时间戳。然而,从我的测试来看,“at”一词似乎将其抛弃,因此您需要使用
replace()
删除“at”一词,然后将其放入Date.parse ()
来获取时间戳。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 usereplace()
to remove the word "at", then put it throughDate.parse()
to get the timestamp.