You are running into a date formatting issue. You are comparing days without a leading zero: "2018-02-9" to days with a leading zero: "2018-02-09".
So since you are only comparing the first digit to "9", EVERY date will be less than 9, except for 9, which will match. When you compare "20" the comparison will not be with "20" but with "2", and it return a result of PAST.
What you want to do here is harmonize your date formatting. A good best practice when working with dates in code is to use leading zeros in dates precisely to avoid this issue. If you are getting single digit inputs, you can convert to double digits using the strtotime
function
There are better ways to increment days than the concatenating loop you are using here (see here for an example), but I don't want to confuse you with extra code, as I want you to be able to see the difference clearly.
As you can see below, all I did was change your initial first date to a double digit format with a leading zero, and then used strtotime
to change your comparative date to the leading zero format as well:
<?php
$today = date('2018-02-09');
$ym = date('Y-m');
$day_count = date('d');
for ( $day = 01; $day <= $day_count; $day++) {
$date = date('Y-m-d', strtotime($ym.'-'.$day));
if ($today == $date) {
echo "today \r\n";
echo $date. "\r\n";
echo $today. "\r\n";
} elseif ($today > $date) {
echo "PAST \r\n";
echo $date. "\r\n";
echo $today. "\r\n";
} else {
echo "FUTURE \r\n";
echo $date. "\r\n";
echo $today. "\r\n";
}
}
?>
Sandbox here: http://sandbox.onlinephpfunctions.com/code/fae07419c11f38454de9f3d301cfd1b87816ff82