2010-07-21 119 views
10

我有一個包含事件的星期日曆,並且希望用戶無法添加過去幾天的事件。所以我特林使用函數那樣:檢查給定日期是否過去

if(strtotime($this->day) < time()){ // date format is YYYY-MM-DD 
// date is past 
}else{ 
// date is not past 
} 

它似乎工作得很好,但它認爲今天的日期爲過去的一天。我究竟做錯了什麼?

回答

10

時間戳從不包含日期,但始終下降到當前秒。​​將在今天的日期返回0:00,而您正在比較它與現在的比較,如11:12

您可以使用strtotime("$this->day 12:59:59pm");(如果格式爲$this->day允許)或明天使用的時間戳。

+1

或者,使用'的strtotime($這個 - >天) user11977 2010-07-21 09:16:21

+0

謝謝你!這種方式很好! – Luciano 2010-07-21 09:18:04

11

簡單 - >

if(strtotime($this->day) < strtotime(date('Y-m-d'))) 
{ 
    ... 
} 
else 
{ 
    ... 
} 
+0

謝謝你呢!我沒有考慮今天在()函數中設置日期! – Luciano 2010-07-21 09:23:15

+0

strtotime函數...只是做了一個錯字..對不起 – 2010-07-21 09:25:01

+0

如果你想也佔了秒,添加H:我:s strtotime(日期('Y-m-d H:我:S'))' – justinl 2014-12-16 20:04:19

0
if(strtotime($this->day) < mktime(0, 0, 0)){ 
    // date is past 
} else { 
    // date is not past 
}