1321941
1321941

Reputation: 2170

Compare dates to display only future events: PHP

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?

Upvotes: 0

Views: 463

Answers (1)

ply
ply

Reputation: 1141

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.

Upvotes: 5

Related Questions