du060334 2013-01-31 23:17
浏览 54

根据UNIX时间戳选择即将到来的会话

I have a service that shows a specific button when a session is "ready", i.e. 15 minutes beforehand and through the session.

I am doing this by pulling up all recently requested sessions, analyzing their timestamps, and then pulling out a specific ID if that session is upcoming. Here is the code:

$session_check_query = "SELECT * FROM requested_sessions WHERE username_t = '{$_SESSION['username']}'";
$session_check_process = mysql_query($session_check_query);

date_default_timezone_set($_SESSION['timezone']);
$current_time = time();

while ($sessions = mysql_fetch_array($session_check_process)) {
    if ($sessions['time_from_t'] - $current_time <= 900 && $current_time - $sessions['time_from_t'] > 0 && $sessions['accepted'] == 1) {
        $session_id = $sessions['id'];
    }
}

The problem is that when I echo $session_id in the loop it will output 1, 2, 3, 4. This means that it feels that all of my sessions in the database meet this criteria when they don't only one does.

Is there a problem with this code? time_from_t is in UNIX format.

  • 写回答

1条回答 默认 最新

  • doucandiao9180 2013-02-01 03:17
    关注

    One possible problem may arise from the fact that time() is timezone independent - it always returns the UTC time (this fact is buried in the comments). More robust handling of timezones can be handled with the DateTime object:

    $date = new DateTime('now', new DateTimeZone($_SESSION['timezone']));
    $current_time = $date->format("U");
    

    Either way, the next step would be to verify your $current_time variable and the time_from_t to see where the maths is going wrong.

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作