Get the difference between a server side time and

2019-04-11 05:50发布

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.

4条回答
爷、活的狠高调
2楼-- · 2019-04-11 06:30

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.

查看更多
SAY GOODBYE
3楼-- · 2019-04-11 06:39

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;
查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-11 06:42

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

查看更多
Juvenile、少年°
5楼-- · 2019-04-11 06:48

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

查看更多
登录 后发表回答