萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 自動生成月曆元件:支援翻頁、高亮今日、自訂樣式

需要在網頁嵌入互動式月曆?本文提供一個功能完整的 PHP 月曆函式 calendar($month, $year),支援月份切換、今日高亮、上下月灰顯、表格化輸出,並附帶 CSS 樣式建議,可直接整合至專案中。

核心函式:calendar($MM, $YYYY)

function calendar($MM = '', $YYYY = '') {
    // 預設為當月
    if ($MM === '')   $MM = date('m');
    if ($YYYY === '') $YYYY = date('Y');

    // 參數驗證
    if (!checkdate($MM, 1, $YYYY)) return '';

    // 關鍵日期資訊
    $firstDay  = (int)strftime('%w', mktime(0,0,0,$MM,1,$YYYY)); // 0=週日
    $daysInMonth = (int)strftime('%d', mktime(0,0,0,$MM+1,0,$YYYY));
    $today = date('j'); // 今日日期
    $isCurrentMonth = ($MM == date('m') && $YYYY == date('Y'));

    // 上/下月連結參數
    $prevMonth = $MM == 1 ? 12 : $MM - 1;
    $prevYear  = $MM == 1 ? $YYYY - 1 : $YYYY;
    $nextMonth = $MM == 12 ? 1 : $MM + 1;
    $nextYear  = $MM == 12 ? $YYYY + 1 : $YYYY;

    $prevLink = "?month=$prevMonth&year=$prevYear";
    $nextLink = "?month=$nextMonth&year=$nextYear";
    $monthName = strftime('%B %Y', mktime(0,0,0,$MM,1,$YYYY));

    // 輸出 HTML 表格
    $html = '<table class="cal-table">';
    $html .= '<thead><tr>';
    $html .= '<th class="nav"><a href="' . $prevLink . '">‹</a></th>';
    $html .= '<th colspan="5" class="title">' . $monthName . '</th>';
    $html .= '<th class="nav"><a href="' . $nextLink . '">›</a></th>';
    $html .= '</tr></thead>';

    $html .= '<thead><tr class="weekday">';
    foreach (['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] as $d) {
        $html .= '<th>' . $d . '</th>';
    }
    $html .= '</tr></thead>';

    $html .= '<tbody><tr>';

    // 前導空白格
    for ($i = 0; $i < $firstDay; $i++) {
        $html .= '<td class="empty">&nbsp;</td>';
    }

    // 日期格
    for ($day = 1; $day <= $daysInMonth; $day++) {
        $class = 'day';
        if ($isCurrentMonth && $day == $today) $class .= ' today';
        if (($firstDay + $day - 1) % 7 == 0) $class .= ' sunday';
        if (($firstDay + $day - 1) % 7 == 6) $class .= ' saturday';

        $html .= '<td class="' . $class . '">' . $day . '</td>';

        // 換行
        if (($firstDay + $day) % 7 == 0 && $day != $daysInMonth) {
            $html .= '</tr><tr>';
        }
    }

    // 尾部空白格
    $lastCell = ($firstDay + $daysInMonth - 1) % 7;
    for ($i = $lastCell + 1; $i < 7; $i++) {
        $html .= '<td class="empty">&nbsp;</td>';
    }

    $html .= '</tr></tbody></table>';
    return $html;
}

使用範例

<?php
// 直接輸出當月月曆
echo calendar();

// 或指定年月
echo calendar(12, 2025);
?>

建議 CSS 樣式

.cal-table { border-collapse: collapse; width: 100%; max-width: 400px; font-family: sans-serif; }
.cal-table th, .cal-table td { padding: 8px; text-align: center; border: 1px solid #eee; }
.cal-table .weekday { background: #f5f5f5; font-weight: bold; }
.cal-table .nav a { text-decoration: none; color: #333; }
.cal-table .title { font-size: 1.1em; }
.cal-table .empty { background: #fafafa; color: #ccc; }
.cal-table .today { background: #007bff; color: #fff; font-weight: bold; border-radius: 50%; }
.cal-table .sunday { color: #dc3545; }
.cal-table .saturday { color: #007bff; }

擴充功能建議

  • 事件標記:陣列 $events['YYYY-MM-DD'] = '事件標題',輸出時加入 title 屬性或圖示
  • AJAX 翻頁:綁定上/下月連結 click 事件,以 fetch() 取得局部 HTML 替換
  • 多語系:使用 setlocale(LC_TIME, 'zh_TW.utf8') 並配合 strftime('%B') 顯示中文月份
  • 日期選取器:點擊日期回填表單欄位,搭配 input[type=hidden] 儲存 ISO 格式日期
copyright © 萬盛學電腦網 all rights reserved