可能重复: 制作直播时钟javascript
我想创建一个时钟, javascript和php。 它应该从服务器时间(使用php的unix时间戳)开始并继续使用javascript。 如何创建它?
I want to create a clock, with javascript and php. It should start from a server time (unix timestamp with php) and continue with javascript. How can I create it?
在这个话题,他说使用 Date.now(); ,但这个函数返回一个时间戳以毫秒为单位,而php不会返回毫秒。
In this topic, he said to use Date.now();, but this function return a timestamp with milliseconds, and php doesn't return milliseconds.
请帮帮我。
谢谢。
推荐答案您必须将当前服务器时间传递到页面中,并使用它来初始化您的javascript时钟。值得庆幸的是,JS的Date对象将接受一个时间戳(以毫秒为单位)进行初始化,因此它很简单:
You'll have to pass the current server time into the page as it's created, and use that to initialize your javascript clock. Thankfully, JS's Date object will accept a timestamp (in milliseconds) to initalize with, so it's as simple as:
<script type="text/javascript"> var d = new Date(<?php echo time() * 1000 ?>); </script>注意 * 1000 部分。 PHP的时间戳以秒为单位,JS以毫秒为单位。
Note the * 1000 part. PHP's timestamps are in seconds, and JS's are in milliseconds.
更多推荐
javascript时钟与服务器时间
发布评论