0

I have the date in is this format:

June 22, 2012

Using PHP's date function I am getting the date like so:

date("F j, Y")

Using an if statement I compare the two hoping to eliminate all of those dates which have already passed:

if(date("F j, Y") > $date)

However, it works but leaves all but one date:

February 14, 2012

Can someone explain why or tell me a better way to do this?

1321941
  • 2,139
  • 5
  • 26
  • 49
  • http://stackoverflow.com/questions/7038484/how-to-compare-two-dates-in-php-and-echo-newer-one?lq=1 – Bailey Parker Jul 17 '12 at 04:51
  • 1
    Don't compare strings, you'll get silly stuff like `Feb 2012` being greater than `Dec 2012`, because F comes after D in the alphabet. Compare native PHP timestamps - simple integers. – Marc B Jul 17 '12 at 04:52

1 Answers1

5

date() returns a string, so in your case its only returns strings that are greater than the literal string "June 22, 2012". Try using strtotime() on your date() call, e.g

$today = strtotime($todays_date);

This will take the time string returned by date() and convert it to unix timestamp, which you can use to compare dates.

ply
  • 1,141
  • 1
  • 10
  • 17