Search code examples
phpdatecompare

Compare current date as Y-m-d against Y-m-d string which doesn't have zero padded days


How can I compare two dates in PHP?

The date is stored in the database in the following format

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?


Solution

  • in the database the date looks like this 2011-10-2

    Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.