从时间中减去一小时()


Remove one hour from time()

我有一个在线系统,需要一个用户在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();

现在那个代码不起作用了,我不认为。

使用strtotime()可以方便地使用

每次用户进行req时,都像这样声明会话。

$_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");

确保每个页面都启动了会话

工作正常。。。

如果您只是想看看$_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) {
}

您需要转换如下所示的时间

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

尝试使用以下代码:

$cut = time() - 60*60;

替换为低于

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