【问题标题】:Remove one hour from time()从 time() 中删除一小时
【发布时间】:2014-10-10 13:07:44
【问题描述】:

我有一个在线系统,需要用户在闲置 60 分钟后退出。

现在当前的代码计算出少了 30 分钟。

    $cut = time() - 30*60;

现在我原以为以下内容会删除一个小时,因此超时为 60 分钟。

$cut = time() - 60*60;

这是发生的完整代码。在 60 分钟不活动后,我需要将该人注销。

// Time out Users 60 minutes
    $last = $_SESSION['time'];
    $cut = time() - 60*60;

    if ($last < $cut)
        {
            session_destroy();

            $get = $_GET;
            $location = 'login.php?timeout=true';
            foreach($get as $key => $item)
                {
                    $location .= '&'.$key."=".$item;
                }
            header('Location: '.$location);
        }
    else
    $_SESSION['time'] = time();

现在我不认为该代码不起作用。

【问题讨论】:

标签: php session time


【解决方案1】:

使用 strtotime() 可能会派上用场

每次用户提出请求时都像这样声明会话。

$_SESSION['time'] = strtotime("now");

编码部分

$last = $_SESSION['time']; 

$cut = strtotime("-60 min");


if ($last < $cut)  //can also use strtotime("-60 min") directly
    {
       // session_destroy();
        unset($_SESSION['time']);  //use inorder to delete only that session


        $get = $_GET;
        $location = 'login.php?timeout=true';
        foreach($get as $key => $item)
            {
                $location .= '&'.$key."=".$item;
            }
        header('Location: '.$location);
    }
else
$_SESSION['time'] = strtotime("now");

确保在每个页面都启动会话。

工作正常...

【讨论】:

    【解决方案2】:

    如果您只想查看$_SESSION['time'] 过期后是否超过一个小时,只需在该时间上增加一个小时并与现在进行比较。如果比现在少,那是一个多小时前。

    $now  = new DateTime(); 
    $last = new DateTime('@' . $_SESSION['time']);
    $last->modify('+1 hour');
    if ($last < $now) {
    
    }
    

    或者从现在开始减去一个小时,看看是否仍然大于$_SESSION['time']

    $now  = new DateTime(); 
    $now->modify('+1 hour');
    $last = new DateTime('@' . $_SESSION['time']);
    if ($last < $now) {
    
    }
    

    【讨论】:

      【解决方案3】:

      您需要转换如下给出的时间

      $cut = time();
      $lesstime = date('YOUR TIME FORMATE',strtotime($cut,"-1 hours"));
      

      【讨论】:

        【解决方案4】:

        试试下面的代码:

        $cut = time() - 60*60;
        

        替换为下面

        $cut = strtotime('-1 hour');
        

        【讨论】:

          猜你喜欢
          • 2011-07-30
          • 1970-01-01
          • 1970-01-01
          • 2014-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多