How can I compare two dates in PHP?
In the database, the date looks like 2011-10-2.
If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it?
I tried this,
$today = date("Y-m-d");
$expire = $row->expireDate //from db
if($today < $expireDate) { //do something; }
but it doesn't really work that way. What's another way of doing it?
Update: I know this post is kinda old but i just wanted to mention carbon, which is a class thats used with laravel but can be used with classic php and it does wonders with dates. check it out: Carbon
If all your dates are posterior to the 1st of January of 1970, you could use something like:
If you are using PHP 5 >= 5.2.0, you could use the DateTime class:
Or something along these lines.
Just to compliment the already given answers, see the following example:
Update: Or simple use old-school date() function:
Found the answer on VipDomaine blog and it's as simple as:
And you'll get the days between the 2 dates.
Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.