显示年历

displaying a year calendar

我是个新手....而且我还在搞乱代码。

我做了一个数组:

$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

然后我有一个 switch 语句 returns 每个月的天数:

function month_length($month) {

    $leap_year = date("L");

    switch ($month) { 
        case "January":    return $max = 31;       
        case "February":   
               if ($leap_year === 1) {
                return $max = 29;
            } else {
                return $max = 28;
            }
        case "March":      return $max = 31;
        case "April":      return $max = 30; 
        case "May":        return $max = 31; 
        case "June":       return $max = 30; 
        case "July":       return $max = 31; 
        case "August":     return $max = 31; 
        case "September":  return $max = 30; 
        case "October":    return $max = 31; 
        case "November":   return $max = 30;
        case "December":   return $max = 31;
        default:           return $max = "wrong number";
    }     
}

有什么方法可以比较数组的任何对象是否与任何大小写匹配?

让我澄清一下;所以目前,我需要设置一个 $current_month 来让 php 知道我要显示什么情况,并且我希望能够显示每年的所有月份。

有人知道我想说什么吗?所以我想我需要将数组 months 中的字符串与 switch 语句的大小写进行比较,但是......如何?因为我不想一个一个比较 :S

谢谢!!!

foreach 的简单解决方案:

foreach($months as $month) {
   echo "The month with name $month has ".month_length($month)." days.<br>";
}

此代码将打印 table 日历。它将每行显示 7 天,共 5 列。从 30/31 到 35 的天数用该月的前几天或后几天填充,以保持星期方面。

希望对您有所帮助。

 function renderMonth($displayM, $displayY){

        $daysOfWeek = array (
                'Mon',
                'Tue',
                'Wen',
                'Thu',
                'Fri',
                'Sat',
                'Sun'
        );

        $dateUtil = new DateTime ( $displayY . "/" . $displayM . "/01"  );
        $year = $dateUtil->format ( "Y" );
        $week = $dateUtil->format ( "W" );

        $week_start = new DateTime ();
        $week_start->setISODate ( $year, $week );
        $nextDay = clone $week_start;

        $i = 1;
        $weekdays = 7; // how many days do we display per row
        $currday = 1; // current week day
        $daysno = 35; // number of display dates

        $calendar = '<table class="event-calendar">';
        $calendar .= "<thead>";
        $calendar .= "<tr>";
        foreach ( $daysOfWeek as $day ) {
            $calendar .= "<th>$day</th>";
        }
        $calendar .= "</tr>";
        $calendar .= "</thead>";
        $calendar .= "<tbody>";
        while ( $i < $daysno ) {
            if ($i == 1) {
                $calendar .= '<tr>';
                $calendar .= '<td>' . $nextDay->format ( 'd M' ) . '</td>';
            }
            $currday ++;
            if ($currday > $weekdays) {
                $calendar .= '</tr>';
                $calendar .= '<tr>';
                $currday = 1;
            }
            $nextDay->add ( new DateInterval ( 'P1D' ) );
            $calendar .= '<td>' . $nextDay->format ( 'd M' ) . '</td>';
            $i ++;
            if ($i == $daysno) {
                $calendar .= '</tr>';
            }
        }
        $calendar .= "</tbody>";
        $calendar .= "</table>";
        return $calendar;
    }


    echo renderMonth("10", "2015");
    echo renderMonth("11", "2015");
    echo renderMonth("12", "2015");