Php 会话变量不能实时工作
我有一个用户注释系统,其中注释时间使用存储在数据库中的UTC时间戳和注释一起显示 时间偏移用于使用javascript post请求设置会话时间偏移变量以获取用户本地时间 我这样做是因为用户来自不同的时区,我不能单独在一个时区中存储时间,所以我将时间存储在UTC中,然后使用时区的时间偏移量显示 该系统不是实时工作,而是在刷新后工作 comments.phpPhp 会话变量不能实时工作,php,javascript,Php,Javascript,我有一个用户注释系统,其中注释时间使用存储在数据库中的UTC时间戳和注释一起显示 时间偏移用于使用javascript post请求设置会话时间偏移变量以获取用户本地时间 我这样做是因为用户来自不同的时区,我不能单独在一个时区中存储时间,所以我将时间存储在UTC中,然后使用时区的时间偏移量显示 该系统不是实时工作,而是在刷新后工作 comments.php <?php session_save_path('session/store'); session_start(); ?>
<?php
session_save_path('session/store');
session_start();
?>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
var timeoffset = new Date().getTimezoneOffset();
$.post('timeoffset.php', {timeoffset:timeoffset}, function(data){
//alert(data);
});
</script>
<?php
if(isset($_SESSION['timeoffset'])&&!empty($_SESSION['timeoffset'])){
date_default_timezone_set('UTC');
$timeoffset = (int)$_SESSION['timeoffset'];
}
else{
echo 'timeoffset not set';
exit;
}
// here are comments displayed using while loop
// time of comments is displayed like this
echo gmdate("F j, Y, h:i:s a", $row['timestamp']-($timeoffset*60)); //$row['timestamp'] = UTC time stamp stored in database. e.g: 1377509788
?>
<?php session_save_path('session/store');
session_start();
if(isset($_POST['timeoffset'])){
$_SESSION['timeoffset'] = (int)$_POST['timeoffset'];
echo $_SESSION['timeoffset'];
}
?>
var timeoffset=new Date().getTimezoneOffset();
$.post('timeoffset.php',{timeoffset:timeoffset},函数(数据){
//警报(数据);
});
timeoffset.php
<?php
session_save_path('session/store');
session_start();
?>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
var timeoffset = new Date().getTimezoneOffset();
$.post('timeoffset.php', {timeoffset:timeoffset}, function(data){
//alert(data);
});
</script>
<?php
if(isset($_SESSION['timeoffset'])&&!empty($_SESSION['timeoffset'])){
date_default_timezone_set('UTC');
$timeoffset = (int)$_SESSION['timeoffset'];
}
else{
echo 'timeoffset not set';
exit;
}
// here are comments displayed using while loop
// time of comments is displayed like this
echo gmdate("F j, Y, h:i:s a", $row['timestamp']-($timeoffset*60)); //$row['timestamp'] = UTC time stamp stored in database. e.g: 1377509788
?>
<?php session_save_path('session/store');
session_start();
if(isset($_POST['timeoffset'])){
$_SESSION['timeoffset'] = (int)$_POST['timeoffset'];
echo $_SESSION['timeoffset'];
}
?>
它首先显示未设置的时间偏移量
,然后在刷新后显示注释和时间
请查看并建议任何可能的方法
谢谢。PHP代码在用户显示页面之前执行。因此,无论代码是在客户端浏览器中执行的javascript代码之前还是之后的文档中,都没有关系。因此,您还必须通过javascript和ajax添加时间。post请求中的alet应该给您正确的结果 当然还没定。在将响应发送给用户并执行javascript之前执行PHP。这意味着您在尝试识别会话后,正在使用jQuery post设置会话。@IanBrindley如何首先发送响应。我不能使用ajax或jquery部分时间刷新注释。是的,警报给出了正确的时间偏移,问题是我不能使用ajax或jquery在注释中刷新部分时间。有没有办法。嗯,为什么不能使用ajax刷新页面上的时间?因为在同一页面中,注释和时间是使用while循环显示的。其他有注释的站点如何在用户本地时间显示注释时间。如果没有更多的源代码,我将无法为您提供太多帮助。使用while循环显示它们意味着什么?