客户端和服务器之间存在时差


Time Difference Exists Between the Client and the Server

Javascript:

function setinvitexpire(){
var ee = new Date ();
ee.setTime(ee.getTime() + (365 * 24 * 3600 * 1000));
var bb = Math.round(new Date().getTime() / 1000) + (24 * 3600); // remove milliseconds
document.cookie="visit=" + bb + ";expires=" + ee.toGMTString();
}
setinvitexpire();

.PHP:

<?php 
if(isset($_COOKIE['visit'])){ 
echo date('m/d/Y g:i A', $_COOKIE['visit']); 
echo "<br>";
} 
?>

我得到:

06/26/2012 11:57 AM

而不是:

06/26/2012 18:07 AM

因为当前时间在我国是 06/25/2012 18:07 AM

我如何解决此问题并简单地将当前时间 + 24 小时保存在 cookie 中?

编辑:我以这种方式解决:

document.cookie="visit=" + <?php echo time() + (24 * 3600); ?> + ";expires=" + ExpireC.toGMTString();

您正在获取 GMT (~ UTC) 时间,您必须同时保存 UTC 时间和时区。查看getTimezoneOffset() : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

附带说明:

toGMTString已被弃用,您应该使用toUTCString