-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
  • 9,687
  • 6
  • 54
  • 105
kathleen55
  • 329
  • 2
  • 9
  • 28

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,211
  • 5
  • 19
  • 29
0
strtotime($date) > strtotime(date('m/d/Y'));
I'm Geeker
  • 4,646
  • 5
  • 20
  • 40
  • 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
  • 62,164
  • 9
  • 85
  • 118