PHP日歷

<?php
02
SimCalendar('2011-08');
03
function SimCalendar($date)
04
{
05
    /**
06
     * 簡單日歷輸出,本函數需要cal_days_in_month的支持
07
     * @param $date Y-m 要輸出的日期
08
     */
09
    echo '<table border="1">
10
    <thead>
11
        <tr>
12
            <th>日</th>
13
            <th>一</th>
14
            <th>二</th>
15
            <th>三</th>
16
            <th>四</th>
17
            <th>五</th>
18
            <th>六</th>
19
        </tr>
20
    </thead>
21
    <tbody>';
22
        $date_array = explode('-', $date);
23
        $start_week = 0;//從星期天開始為0
24
        $month = cal_days_in_month(CAL_GREGORIAN, $date_array[1], $date_array[0]);//當月的天數
25
        $wstar = date('w', strtotime($date . '-01'));//當月從星期幾天始
26
        $rows = ceil(($wstar + $month) / 7);//總行數
27
        $mday = 1;//第幾天
28
        for ($i = 0; $i < $rows; $i++) {
29
            echo '<tr>';
30
            for ($d = 0; $d < 7; $d++) {
31
                $nowday = 7 * $i + $d + $start_week;
32
                if ($nowday >= $wstar && $mday <= $month) {
33
                    $temp = date('d', strtotime($date . '-' . $mday));
34
                    echo '<td>'.$temp . '</td>';
35
                    $mday++;
36
                } else {
37
                    echo '<td>&nbsp;</td>';
38
                }
39
            }
40
            echo '</tr>';
41
        }
42
    
43
    echo '</tbody>
44
</table>';
45
}
46
?>
作者“Flowf的博客”

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *