Guillaume OSTORERO
Guillaume OSTORERO

Reputation: 69

Incredible behaviour with strftime

I have this code:

function calendarDay ($month, $year)
    {
        $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        $today = strtotime(date("Y-m-d"));
        $result = array();
        $str = "";
        $strMonth = "";
        for ($i = 0; $i < $num; $i++) {
            $date = strtotime($year . "-" . $month . "-" . ($i + 1));
            $day=strftime("%a", $date);
            $month = strftime("%b", $date);
            if ($today == $date) {
                if ($day == "Sat" || $day == "Sun") {
                    $str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
                    $strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $month." ". "</th>";
                }
                else {
                    $str .= "<th style='background-color:#888888'>" . $day. "</th>";
                    $strMonth = $strMonth . "<th style='background-color:#888888'>".($i + 1) . "-" . $month." ". "</th>";
                }
            }
            else if ($today != $date) {
                if ($day == "Sat" || $day == "Sun") {
                    $str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
                    $strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $month." ". "</th>";
                }
                else {
                    $str .= "<th>" . $day. "</th>";
                    $strMonth = $strMonth . "<th>".($i + 1) . "-" . $month." ". "</th>";
                }

            }
            $result = array_merge($result, array("Month" => $strMonth));
            $result = array_merge($result, array("Day" => $str));
        }
        return $result;
    }

When I delete the line which convert my numeric $month from parameters to string with strftime("%b", $date), It gives the good behaviour. And when I add this line the var $day began to repeat 9 times the first day of the month... which is Tuesday, and can't get the solution, its a bug for me...

Upvotes: 0

Views: 53

Answers (3)

Guillaume OSTORERO
Guillaume OSTORERO

Reputation: 69

Oh my go thanks you all !!! a lot, i was searching what i'm doing wrong since several days...

Just a var name conflict with my numeric $month and the string month with strftime. So i replaced the name of the string one.

Bad mistake ! ^^

Upvotes: 0

Arun Kumar
Arun Kumar

Reputation: 218

Replace the code

$date = strtotime($year . "-" . $month . "-" . ($i + 1));

into

$date = strtotime($year . "-" . $month . "-" .str_pad(($i + 1), 2, '0', STR_PAD_LEFT) );

Upvotes: 0

Barmar
Barmar

Reputation: 780655

You're replacing the variable that you use in:

$date = strtotime($year . "-" . $month . "-" . ($i + 1));

So on the 2nd day of the month, instead of trying to parse 2019-1-2 you're parsing 2019-Jan-1. I'm not sure why, but when you use this format with a single-digit day, it always parses as if it's 2019-01-01.

The solution is to use a different variable.

function calendarDay ($month, $year)
{
    $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $today = strtotime(date("Y-m-d"));
    $result = array();
    $str = "";
    $strMonth = "";
    for ($i = 0; $i < $num; $i++) {
        $date = strtotime($year . "-" . $month . "-" . ($i + 1));
        $day=strftime("%a", $date);
        $monthName = strftime("%b", $date);
        if ($today == $date) {
            if ($day == "Sat" || $day == "Sun") {
                $str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
                $strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $monthName." ". "</th>";
            }
            else {
                $str .= "<th style='background-color:#888888'>" . $day. "</th>";
                $strMonth = $strMonth . "<th style='background-color:#888888'>".($i + 1) . "-" . $monthName." ". "</th>";
            }
        }
        else if ($today != $date) {
            if ($day == "Sat" || $day == "Sun") {
                $str .= "<th style='background-color:mediumseagreen'>" . $day. "</th>";
                $strMonth = $strMonth . "<th style='background-color:mediumseagreen'>".($i + 1) . "-" . $monthName." ". "</th>";
            }
            else {
                $str .= "<th>" . $day. "</th>";
                $strMonth = $strMonth . "<th>".($i + 1) . "-" . $monthName." ". "</th>";
            }

        }
        $result = array_merge($result, array("Month" => $strMonth));
        $result = array_merge($result, array("Day" => $str));
    }
    return $result;
}

Upvotes: 0

Related Questions