tags:

views:

59

answers:

3

I have a week calendar that holds events, and want that users can't add events for the past days. So I'm tring to use a function like that:

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

It seems to works fine, except that it consider today date as a past day. What am i doing wrong?

+5  A: 

A timestamp never contains only the date, but is always down to the current second. strtotime($this->day) is going to return today's date at 0:00, while you are comparing it against now, say, 11:12.

You could use strtotime("$this->day 12:59:59pm"); (if the format of $this->day allows for that) or use tomorrow's timestamp.

Pekka
Alternately, use `strtotime($this->day)<time()-24*60*60`.
@Pekka: Thanks man! This way works fine!
Luciano
+1  A: 

Simpler ->

if(strtotime($this->day) < strtotime(date('Y-m-d')))
{
   ...
}
else
{
   ...
}
budinov.com
@budinov.com: Thanks you too! I was not considering to set today date in time() function!
Luciano
strtotime function ... just did a typo .. sorry
budinov.com
A: 
if(strtotime($this->day) < mktime(0, 0, 0)){
    // date is past
} else {
    // date is not past
}
Nirmal