-4

I have an if statement that will compare the input date with the system date.

$Date > date('m/d/Y')

The problem is that when I input 12/16/2012 it will throw an error that the date is greater than the date today. I don’t know what the problem in my if statement is. I have a try and catch inside that function that will catch any InputException.

TRiG
  • 10,148
  • 7
  • 57
  • 107
kathleen55
  • 341
  • 2
  • 9
  • 29

3 Answers3

0

Comparing strings will not give you the desired output. You could use strtotime to compare the dates like this: strtotime($date) > date('m/d/Y',time())

Aditya
  • 1,241
  • 5
  • 19
  • 29
0
strtotime($date) > strtotime(date('m/d/Y'));
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
  • Why format the current time using `date()` then parse the formatted string using `strtotime()`? – axiac Dec 15 '14 at 12:46
0

Use strtotime() to parse the value you store in $Date to get a timestamp (i.e. the number of seconds passes since Jan 1, 1970). Then you compare it against the current timestamp, retrieved using the function time()

if (strototime($Date) > time()) {
    echo($Date.' is in the future.');
} else {
    echo($Date.' is in the past.');
}
axiac
  • 68,258
  • 9
  • 99
  • 134