获取服务器端时间和客户端时间之间的差异并显示它

发布于 2024-12-02 14:28:37 字数 449 浏览 0 评论 0原文

可能的重复:
如何将 JavaScript 倒计时与服务器时间同步 < /p>

如何我可以计算服务器端时间和客户端时间之间的差异,然后将其显示在我的视图中,这是一个 PHP 页面。

让我说清楚一点。我有一个特定板球比赛开始的时间数据,该数据存储在数据库中。现在,我想在我的视图(PHP 页面)中显示这个时间与客户端浏览器中的时间之间的差异。我可以理解我需要为此编写一个 javascript 函数,但是我如何从我的 PHP 代码中调用这个函数,并且我必须再次在我的视图中显示差异。

我对此很困惑。

Possible Duplicate:
How to sync a javascript countdown with server time

How can I get the to calculate the difference between the server side time and the client side time and then display it in my view , which is a PHP page .

Let me make this a bit clear. I have a time data for a particular cricket match to start which is stored in the database . Now, I want to display the difference between this time and the time in the client browser in my view which is a PHP page. I can understand that I need to write a javascript function for this , but how can I call this function from my PHP code ,and again I have to display the difference in my view.

I am confused about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假装不在乎 2024-12-09 14:28:37

date.js 对于日期计算来说非常方便,并且可以解析/显示各种格式的日期。

date.js is pretty handy for date calculations and can parse/display dates in various formats.

满意归宿 2024-12-09 14:28:37

将 php 中的日期写入页面上的隐藏输入。以如下格式存储:YYYY/MM/DD hh:mm:ss。通过将隐藏字段的值传递给 Date 构造函数来加载该日期:

var cricketDateField = document.getElementsById("cricketDateField");
var cricketDate = new Date(cricketDateField.value);

只需调用不带参数的 Date 构造函数即可获取当前日期:

var now = new Date();

通过减去日期来获取以毫秒为单位的差异:

var msDiff = cricketDate - now;  // difference in milliseconds

然后您可以手动解析毫秒,或转换到一个日期并提取日期的各个部分以获得差异:

var diff = new Date(msDiff - 62167190400000);  // difference as a date
var years = diff.getYear();
var months = diff.getMonth();
var days = diff.getDate() - 1;
var hours = diff.getHours();
var minutes = diff.getMinutes();
var seconds = diff.getSeconds();
var msg = "There are " + years + " years, " +
                         months + " months, " +
                         days + " days, " +
                         hours + " hours, " +
                         minutes + " minutes, and " +
                         seconds + " seconds until the cricket match.";
document.getElementById("differenceMsg").innerHTML = msg;

Write your date in php to a hidden input on your page. Store it in a format like: YYYY/MM/DD hh:mm:ss. Load that date by passing the value of the hidden field to the Date constructor:

var cricketDateField = document.getElementsById("cricketDateField");
var cricketDate = new Date(cricketDateField.value);

Get the current date just by calling the Date constructor with no arguments:

var now = new Date();

Get the difference in milliseconds by subtracting the dates:

var msDiff = cricketDate - now;  // difference in milliseconds

You can then parse the milliseconds manually, or convert to a date and extract the parts of the date to get the difference:

var diff = new Date(msDiff - 62167190400000);  // difference as a date
var years = diff.getYear();
var months = diff.getMonth();
var days = diff.getDate() - 1;
var hours = diff.getHours();
var minutes = diff.getMinutes();
var seconds = diff.getSeconds();
var msg = "There are " + years + " years, " +
                         months + " months, " +
                         days + " days, " +
                         hours + " hours, " +
                         minutes + " minutes, and " +
                         seconds + " seconds until the cricket match.";
document.getElementById("differenceMsg").innerHTML = msg;
蓝色星空 2024-12-09 14:28:37

我不知道这是否是正确的方法..但您可以做的是

var mytime=new Date();
var serverTime = new Date('<?php echo date('Y/m/d H:i:s')?>');
var difference  = (mytime - serverTime)/1000;
var url = 'setSessionData.php';
$.post(url,'dateDiff='+difference);

在 setSessionData 中只需使用该值并设置会话..并在显示详细信息时获取比赛时间

date('Y-m-d H:i:s',strtotime('+ '.$_SESSION['date_difference'].' sec', $matchTime));

使用 mktime() 来计算 $matchTime

i do not know if it is the correct methods.. but you can do is

var mytime=new Date();
var serverTime = new Date('<?php echo date('Y/m/d H:i:s')?>');
var difference  = (mytime - serverTime)/1000;
var url = 'setSessionData.php';
$.post(url,'dateDiff='+difference);

and in the setSessionData just use the value and set session .. and when showing the details get the match times

date('Y-m-d H:i:s',strtotime('+ '.$_SESSION['date_difference'].' sec', $matchTime));

use mktime() to calculate the $matchTime

别再吹冷风 2024-12-09 14:28:37

将 (new Date()).getTime() 从 javascript 发送到 php 脚本,并使用 php 中的 time() 函数。

$difference = time() - (int)$_GET['clientTime'];

这应该精确到客户端与服务器对话所需的时间。

Send (new Date()).getTime() from javascript to the php script, and use the time() function in php.

$difference = time() - (int)$_GET['clientTime'];

This should be accurate down to the time it takes the client to talk to your server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文