2015-11-13 126 views
0

我想檢查一下我存儲在數據庫中的哈希是否已過期,即大於30分鐘。PHP - 檢查哈希是否已過期

這是我的支票

$db_timestamp = strtotime($hash->created_at); 
$cur_timestamp = strtotime(date('Y-m-d H:i:s')); 
if (($cur_timestamp - $db_timestamp) > ???) { 
    // hash has expired 
} 
+0

由於unix時間戳在secon ds,那麼比較的值是30 * 60 = 1800 –

+1

而你不需要'$ cur_timestamp = strtotime(date('Ymd H:i:s'));'',只需使用'$ cur_timestamp = time() ' –

回答

3

時間戳是秒的數字,所以你要看到,如果散列(以秒爲單位)的年齡比的秒在30分鐘內的數量。

圖有多少秒還有30分鐘:

$thirtymins = 60 * 30; 

然後使用該字符串:

if (($cur_timestamp - $db_timestamp) > $thirtymins) { 
    // hash has expired 
} 

您還可以清理代碼執行以下操作:

$db_timestamp = strtotime($hash->created_at); 
if ((time() - $db_timestamp) > (30 * 60)) { 
    // hash has expired 
}