如何使php日历动态化

时间:2014-06-19 10:56:49

标签: php

我的日历根据需要很好地显示,但日期不会改变。当一个月是31天时,它仍然是30,并且始终从星期二开始。它只是静态的。下面是构建日历的代码

<?php
/* draws a calendar */
function draw_calendar($month,$year){

    /* draw table */
    $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

    /* table headings */
    $headings = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
    $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

    /* days and weeks vars now ... */
    $running_day = date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 0;
    $dates_array = array();

    /* row for week one */
    $calendar.= '<tr class="calendar-row">';

    /* print "blank" days until the first of the current week */
    for($x = 0; $x < $running_day; $x++):
        $calendar.= '<td class="calendar-day-np"> </td>';
        $days_in_this_week++;
    endfor;

    /* keep going with days.... */
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td class="calendar-day">';
            /* add in the day number */
            $calendar.= '<div class="day-number">'.$list_day.'</div>';

            /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
            $calendar.= str_repeat('<p> </p>',2);

        $calendar.= '</td>';
        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr class="calendar-row">';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
    endfor;

    /* finish the rest of the days in the week */
    if($days_in_this_week < 8):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<td class="calendar-day-np"> </td>';
        endfor;
    endif;

    /* final row */
    $calendar.= '</tr>';

    /* end the table */
    $calendar.= '</table>';

    /* all done, return result */
    return $calendar;
}

/* sample usages */
echo "<h2>".date('F, Y')."</h2>";
echo draw_calendar(4,2014);
echo "<br /><hr/>";
?>

1 个答案:

答案 0 :(得分:1)

function draw_calendar($m,$y){
    $date=strtotime("01-".$m."-".$y);
    $day=date('d',$date);
    $month=date('m',$date);
    $year=date('Y',$date);
    $first_day=mktime(0,0,0,$month, 1,$year);
    $title=date('F',$first_day);
    $day_of_week=date('D',$first_day); 
    $blankCount=array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
    $blank=array_search($day_of_week,$blankCount);
    $days_in_month=cal_days_in_month(0,$month,$year);
    $CalenderTable="<table border=1 width=294>";
    $CalenderTable.="<thead><th><<</th><th colspan=5><span class='Title'>$title</span><span>$year<span></th><th>>></th></thead>";
    $CalenderTable.="<tr><td>Su</td><td>Mo</td><td>Tu</td><td>We</td><td>Th</td><td>Fr</td><td>Sa</td></tr>";
    $day_count=1;
    $CalenderTable.="<tr>";
    while ( $blank > 0 ) { 
        $CalenderTable.="<td class='past blank'></td>"; 
        $blank--; 
        $day_count++;
    }
    $day_num=1;
    while ( $day_num <= $days_in_month ) { 
        $CalenderTable.="<td>$day_num</td>"; 
        $day_num++; 
        $day_count++;
        if ($day_count > 7) {
            $CalenderTable.="</tr><tr>";
            $day_count=1;
        }
    } 
    while ( $day_count >1 && $day_count <=7 ) { 
        $CalenderTable.="<td class='future blank'> </td>"; 
        $day_count++; 
    } 
    $CalenderTable.="</tr></table>";
    return $CalenderTable;
}
echo draw_calendar(11,1991);