我的时差是否准确?

时间:2013-08-23 00:47:40

标签: php time unix-timestamp date-difference

我正在进行密码重置,而我正处于检查令牌是否在时间范围内的部分。

这是我的代码:

            $tokentime = substr($dbtoken[0], 0, 4); // get the DB time that I store with date('hi') . $token
            $now = date('hi'); // Create the now time in same format as $tokentime

            var_dump($now); // This generates : 0838 (couple mines ago) 
            var_dump($tokentime); this generates : 0652 (102 minutes ago)
            var_dump(strtotime($now)); // convert 8380 to unix time
            var_dump(strtotime($tokentime)); // convert 0652 into unix time

            if (strtotime($now) - strtotime($tokentime) >= 600) { // If the unix seconds are equal to or greater then 600 seconds 
                echo "Token Expired";
            } else {
                echo "Not Expiered";
            }

我的思维方式是否正确?

这是var_dump值:

  
    

string(4)“0838”string(4)“0652”int(1377175080)int(1377168720)令牌已过期

  

我将它设置为在10分钟600秒内过期。

这是我第一次检查时间差异,并将日期值存储在令牌中。我只是想知道这是否是找到时间差异的正确方法。

1 个答案:

答案 0 :(得分:0)

这很简单: 在您的令牌中的某个时刻time()值。后:

if (time()-tokentime) > 600)
{
    // expired
}

time()将为您提供所谓的UNIX时间戳,它与时区无关。