我的客户喜欢为他们的网络用户提供的网站功能之一是在线动态日历。在线日历可用于活动、即将推出的产品特价、备忘录以及您能想到的任何其他内容。我花了一些时间完全重写了 PHP 事件日历,以便与您分享。
CSS
/* calendar */ table.calendar { border-left:1px solid #999; } tr.calendar-row { } td.calendar-day { min-height:80px; font-size:11px; position:relative; } * html div.calendar-day { height:80px; } td.calendar-day:hover { background:#eceff5; } td.calendar-day-np { background:#eee; min-height:80px; } * html div.calendar-day-np { height:80px; } td.calendar-day-head { background:#ccc; font-weight:bold; text-align:center; width:120px; padding:5px; border-bottom:1px solid #999; border-top:1px solid #999; border-right:1px solid #999; } div.day-number { background:#999; padding:5px; color:#fff; font-weight:bold; float:right; margin:-5px -5px 0 0; width:20px; text-align:center; } /* shared */ td.calendar-day, td.calendar-day-np { width:120px; padding:5px; border-bottom:1px solid #999; border-right:1px solid #999; }
上面的代码是完整的 IE6 hack。
PHP
/* draws a calendar */ function draw_calendar($month,$year){ /* draw table */ $calendar = '<table cellpadding="0" cellspacing="0" >'; /* table headings */ $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $calendar.= '<tr ><td >'.implode('</td><td >',$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 >'; /* print "blank" days until the first of the current week */ for($x = 0; $x < $running_day; $x++): $calendar.= '<td > </td>'; $days_in_this_week++; endfor; /* keep going with days.... */ for($list_day = 1; $list_day <= $days_in_month; $list_day++): $calendar.= '<td >'; /* add in the day number */ $calendar.= '<div >'.$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 >'; 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 > </td>'; endfor; endif; /* final row */ $calendar.= '</tr>'; /* end the table */ $calendar.= '</table>'; /* all done, return result */ return $calendar; } /* sample usages */ echo '<h2>July 2009</h2>'; echo draw_calendar(7,2009); echo '<h2>August 2009</h2>'; echo draw_calendar(8,2009);
PHP 主要基于一个函数,该函数只需要您想要的日历中的月份和年份。这是一个相当大的功能,但显然物有所值。另请注意,我已经在日历中确定了一个位置,您应该在该位置查询数据库以查看当天是否有任何事件。我使用表格是因为当一周中的某一天比其他天长时,它们可以很好地伸展。在日历中使用绝对定位和 DIV 对于简单的日历来说太麻烦了。
我期待看到您可以使用日历做什么!