javascript时钟与服务器时间 [英] javascript clock with server time

查看:172
本文介绍了javascript时钟与服务器时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

制作直播时钟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时钟与服务器时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆