2010-09-19 55 views
0

我已經創建了一個函數來確定「offer」是否當前有效。每個報價都有一個存儲在數據庫中的開始和結束日期,我使用date()函數來判斷它是否有效。但是,由於以下情況不會返回「TRUE」,所以出現問題。我相信它應該返回「真」。我沒有得到什麼?使用date()函數提供有效性

$start = 2010-9-18 
$stop = 2010-10-10 
// Current date is 2010-9-19 

function is_active($start, $stop) { 

$now = date("Y-n-d"); 

if($now >= $start && $now <= $stop) { 
return true; 
} 
} 
?> 

感謝您的幫助!

+1

您在您的日期附近失去了報價。但是更重要的是:你需要驗證1970年1月1日至2038年之間的日期嗎? – 2010-09-19 20:58:25

+0

不,那不需要:-) – 2010-09-20 16:12:35

回答

2

date()返回一個字符串。字符串通過字典順序進行比較。在此訂購中,9大於10,因爲1<anything>出現在9<anything>之前。

一個簡單的解決方法是使用Y-m-d而不是Y-n-d,並將$start記錄爲'2010-09-18'

$start = '2010-09-18'; 
$stop = '2010-10-10'; 
// Current date is 2010-9-19 

function is_active($start, $stop) { 

$now = date("Y-m-d"); 

if($now >= $start && $now <= $stop) { 
return true; 
} 
} 
+0

工程!非常感謝! – 2010-09-19 21:17:49

相關問題