javascript时钟与服务器时间

编程入门 行业动态 更新时间:2024-10-10 21:21:25
本文介绍了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时钟与服务器时间

本文发布于:2023-11-27 14:20:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时钟   服务器   时间   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!