javascript clock with server time

Possible Duplicate:
Making Live Clock 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?

In this topic, he said to use Date.now(); , but this function return a timestamp with milliseconds, and php doesn't return milliseconds.

Help me, please.

Thanks.


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>

Note the * 1000 part. PHP's timestamps are in seconds, and JS's are in milliseconds.


PHP函数返回毫秒:http://php.net/manual/en/function.microtime.php


I did this myself and posted it on github if it can be useful:

https://github.com/Lwangaman/jQuery-Clock-Plugin

It uses jquery, it's basically a jquery plugin, and it can take a server timestamp to start with. The css can also be easily modified to one's own likings.

链接地址: http://www.djcxy.com/p/86170.html

上一篇: 客户端有什么区别

下一篇: JavaScript时钟与服务器时间