I want to compare two dates in PHP.
One date is the date due, which is provided by the user, stored in a database and retrieved with PHP.
The second is today’s date.
$unixdue = strtotime($query['date_due']); //Converts the database integer to Unix date
$duestamp = date("dmY", $unixdue); //Converts the Unix date to dd-mm-yyyy
//If the due date is larger than today (i.e., after today), it's early.
if ($query['complete'] == 0 AND date("dmY") < $duestamp) {echo "Early";}
//If the due date is smaller than today (i.e., before today), it's late.
elseif ($query['complete'] == 0 AND date("dmY") > $duestamp) {echo "Late";}
//If the due date IS today, it's today.
elseif ($query['complete'] == 0 AND date("dmY") == $duestamp) {echo "Today";}
It works with dates that are today, but all the others return “Early” even if they are not early.
Due to this I can’t even tell if the rest is working at all.
I have tried comparing two Unix dates, but they include the time as well, when really I only want the date. If I compare two unix dates, I can’t say if something is “Today”.
You can’t compare whether two strings are less than or greater than each other. Or you can, but in this case you shouldn’t because the results are not what you want (“03092011” < “29082011” for example).
You can first compare if the dates are equal as you have done; if they are not, you can compare the timestamps. Even though you use only dates it doesn’t matter because the timestamp of an earlier date is always smaller than the timestamp of a later date, regardless of the time part.