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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度