2016-05-07 37 views
0

我有一個PHP腳本來檢查數據庫中存儲的日期是否大於,等於或小於當前時間。檢查數據庫中的日期是否大於當前時間

  • 如果它大於或等於,則回聲'較大'。
  • 如果不是更大,呼應「而不是」

我的問題是,即使在數據庫時間不到當前的時間,它的回聲「大」(大於或等於當它應該回聲'不')。

存儲在數據庫中日期:

enter image description here

date_default_timezone_set('Europe/London'); 
$time_stamp = date('Y-m-d H:i:s'); 

$stmt = $con->prepare("SELECT * FROM table"); 
$stmt->execute(); 

$row = $stmt->fetch(); 
if($row['BannedUntil'] <= $time_stamp) { 
echo 'Greater'; 
}else{ 
echo 'not'; 
} 
+1

不比較字符串:'如果(的strtotime($行['BannedUntil'])<= time()){' – splash58

+0

@ splash58謝謝。利用這一點,我能夠解決這個問題。 :) –

回答

0

使用MySQL日期和時間函數DATEDIFF

date_default_timezone_set('Europe/London'); 
$time_stamp = date('Y-m-d H:i:s'); 

$stmt = $con->prepare("SELECT DATEDIFF(`BannedUntil`,NOW()) AS `daysUntil` FROM table"); 
$stmt->execute(); 

$row = $stmt->fetch(); 
if($row['daysUntil'] >= 0) { 
echo 'Greater'; // banned 
}else{ 
echo 'not'; // not banned 
} 
+0

這是必要的,因爲'if(strtotime($ row ['BannedUntil'])<= time()){'似乎工作正常嗎? –

+0

檢查這個小提琴http://sqlfiddle.com/#!9/9eecb7d/58348。結果是表示天數的整數。它可以是正數,負數或等於零。這是對我的看法更準確的方法。你有沒有測試過它,看它是否回答你的問題? –

相關問題