Невероятное поведение с strftime - PullRequest
0 голосов
/ 10 января 2019

У меня есть этот код:

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;
    }

Когда я удаляю строку, которая преобразует мой числовой $ month из параметров в строку с strftime ("% b", $ date), это дает хорошее поведение. И когда я добавляю эту строку, переменная $ day начала повторяться 9 раз в первый день месяца ... это вторник, и я не могу найти решение, для меня это ошибка ...

Ответы [ 3 ]

0 голосов
/ 10 января 2019

Заменить код

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

в

$date = strtotime($year . "-" . $month . "-" .str_pad(($i + 1), 2, '0', STR_PAD_LEFT) );
0 голосов
/ 10 января 2019

Ой мой, спасибо всем вам !!! много, я искал то, что я делаю не так уже несколько дней ...

Просто имя переменной конфликтует с моим числовым месяцем и строковым месяцем с strftime. Поэтому я заменил имя строки один.

Плохая ошибка! ^^

0 голосов
/ 10 января 2019

Вы заменяете переменную, которую используете в:

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

Итак, во 2-й день месяца вместо попытки анализа 2019-1-2 вы анализируете 2019-Jan-1. Я не уверен, почему, но когда вы используете этот формат с однозначным днем, он всегда анализируется, как если бы он был 2019-01-01.

Решение состоит в том, чтобы использовать другую переменную.

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;
}
...