2

The php code below will display all days as PAST, except for the current day. I originally thought it was only checking the first digit, so placing a 2 instead of the 9 in the days coloumn would have yield every day past 20 being a "today" day, however that didnt happen.

http://sandbox.onlinephpfunctions.com/code/fae07419c11f38454de9f3d301cfd1b87816ff82

<?php
$today = date('2018-02-9');
$ym = date('Y-m');
$day_count = date('t');

for ( $day = 1; $day <= $day_count; $day++) {

$date = $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";
  }


}

4 Answers4

1

date() returns a string so your comparisons are string comparison, not numeric (or date) comparisons. If you look carefully, you'll notice that $today is missing a leading zero in the day, so the comparison works unexpectedly (remember, it's a string comparison). For example:

$d1 = "2018-02-9";
$d2 = "2018-02-10";
var_dump($d1 > $d2); // true

$d1 = "2018-02-09";
$d2 = "2018-02-10";
var_dump($d1 > $d2); // false

Because, the comparison gets to the 9 in the first string and evaluates it as bigger than 1 in the second string.

You can fix it by either adding a 0 before the 9 and keep your string comparisons (which will still give unexpected results), or convert the dates to timestamps and do a proper numeric comparison:

<?php
$today = strtotime(date('2018-02-09'));
$ym = strtotime(date('Y-m'));
$day_count = date('t');

for ( $day = 1; $day <= $day_count; $day++) {
    $date = strtotime(date($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";
    }
}

Demo

ishegg
  • 9,685
  • 3
  • 16
  • 31
0

You don't need to bother with converting all of the dates to timestamps, if you just ensure that all dates are in the Y-m-d format.

Code: (Demo)

$today = date('2018-02-09');  // fix your input value to be the standard Y-m-d format
$ym = date('Y-m');
$day_count = date('t');

for ( $day = 1; $day <= $day_count; ++$day ) {
    $date = $ym.'-'.str_pad($day,2,"0",STR_PAD_LEFT);  // just padd the incremented day variable
    if ($today > $date) {
        echo "PAST ";
    } elseif ($today < $date) {
        echo "FUTURE";
    } else {
        echo "TODAY";
    }
    echo " $date & $today\n";
}

Output:

PAST  2018-02-01 & 2018-02-09
PAST  2018-02-02 & 2018-02-09
PAST  2018-02-03 & 2018-02-09
PAST  2018-02-04 & 2018-02-09
PAST  2018-02-05 & 2018-02-09
PAST  2018-02-06 & 2018-02-09
PAST  2018-02-07 & 2018-02-09
PAST  2018-02-08 & 2018-02-09
TODAY 2018-02-09 & 2018-02-09
FUTURE 2018-02-10 & 2018-02-09
FUTURE 2018-02-11 & 2018-02-09
FUTURE 2018-02-12 & 2018-02-09
FUTURE 2018-02-13 & 2018-02-09
FUTURE 2018-02-14 & 2018-02-09
FUTURE 2018-02-15 & 2018-02-09
FUTURE 2018-02-16 & 2018-02-09
FUTURE 2018-02-17 & 2018-02-09
FUTURE 2018-02-18 & 2018-02-09
FUTURE 2018-02-19 & 2018-02-09
FUTURE 2018-02-20 & 2018-02-09
FUTURE 2018-02-21 & 2018-02-09
FUTURE 2018-02-22 & 2018-02-09
FUTURE 2018-02-23 & 2018-02-09
FUTURE 2018-02-24 & 2018-02-09
FUTURE 2018-02-25 & 2018-02-09
FUTURE 2018-02-26 & 2018-02-09
FUTURE 2018-02-27 & 2018-02-09
FUTURE 2018-02-28 & 2018-02-09
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • But over-complicates this task. Appropriately preparing the data for these simple comparisons just makes good sense. – mickmackusa Feb 19 '18 at 04:27
0

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

Sharky Laguana
  • 157
  • 1
  • 6
0

As I understand you are trying to display all days in the current month before $today as PAST dates and all days after $today in the current month as FUTURE dates and the $today date as TODAY.

Your code seems right, but the '<' and '>' comparisons are applied to numbers and not strings. You need to convert the dates to timestamp using strtotime function.

Nadir Latif
  • 3,690
  • 1
  • 15
  • 24