我做了一个类似于你要求的功能。它基本上使用表格生成日历,您可以传递一个由日期构成的可选数组(即 2013-06-22),因此它会期望一个类似于此的数组:
$array['2013-06-22']['data']
然后,您可以自定义函数并根据需要显示数据。或者你可以简单地在函数中执行你的查询(函数中应该有一个注释来提示你可以在哪里运行你的查询)..我还记得那里有很多评论..我太懒了删除,所以你可能想清理一下。
老实说,这个功能是一个旧版本,我有一个改进的版本,但似乎无法在 atm 找到它。
希望你觉得它有用 :)
/**
* Display Calendar (and Optional Data structured by date)
* @author: Zubair Baig
*/
function show_calendar_old($month = '', $year = '', $dataByDate = '')
{
$listOfDaysInWeek = array('01'=>'Sunday', '02'=>'Monday',
'03'=>'Tuesday', '04'=>'Wednesday',
'05'=>'Thursday', '06'=>'Friday',
'07'=>'Saturday');
$month = !empty($month) ? $month : date('m', time());
$year = !empty($year) ? $year : date('y', time());
$dataByDate = is_array($dataByDate) ? $dataByDate : array();
$calendar = '<table class="calendar">'.PHP_EOL;
//$days_in_month = days_in_month($month, $year);
$days_headings = $listOfDaysInWeek;
//Table Head
$calendar.= '<thead>'.PHP_EOL;
$calendar.= '<tr class="calendar-row">'.PHP_EOL;
$calendar.= '<th class="calendar-day-head">';
$calendar.= implode('</th>'.PHP_EOL.'<th class="calendar-day-head">', $days_headings);
$calendar.= '</th>'.PHP_EOL;
$calendar.= '</tr>'.PHP_EOL;
$calendar.= '</thead>'.PHP_EOL;
//Table Foot
$calendar.= '<tfoot>'.PHP_EOL;
$calendar.= '<tr class="calendar-row">'.PHP_EOL;
$calendar.= '<td class="calendar-day-head">';
$calendar.= implode('</td>'.PHP_EOL.'<td class="calendar-day-head">', $days_headings);
$calendar.= '</td>'.PHP_EOL;
$calendar.= '</tr>'.PHP_EOL;
$calendar.= '</tfoot>'.PHP_EOL;
$days_counter = 0;
//$running_day = date('w', strtotime('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_week = 7;
$days_in_this_week = 1;
//var_dump( $days_in_month, $running_day );
$dates_array = array();
//Table Body
$calendar.= '<tbody>'.PHP_EOL;
/* row for week one */
$calendar.= '<tr>'.PHP_EOL;
/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++)
{
$calendar.= '<td> </td>'.PHP_EOL;
$days_in_this_week++;
}
/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++)
{
$classes = '';//reset
if (isset($dataByDate) && is_array($dataByDate))
{
$runningCalDateTimeStamp = mktime(0,0,0,$month,$list_day,$year);
$runningCalDate = date('Y-m-d', $runningCalDateTimeStamp);
if (in_array($runningCalDate, $dataByDate))
{
if (date('Y-m-d', time()) > $runningCalDate)
{
$classes = ' day-can-be-reserved-but-old';
} else {
$classes = ' day-can-be-reserved';
}
} else {
$classes = ' day-cannot-be-reserved';
}
if (date('Y-m-d', time()) == $runningCalDate)
{
$classes .= ' day-is-today';
}
}
$calendar.= '<td class="calendar-day' .$classes. '">'.PHP_EOL;
/* add in the day number */
$calendar.= '<div class="day-number">'.$list_day.'</div>'.PHP_EOL;
/** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !! IF MATCHES FOUND, PRINT THEM !! **/
$calendar.= str_repeat('<input type="checkbox" name="selectedDates" value="' .day_with_zero($list_day). '" />'.PHP_EOL,1);
//var_dump(in_array( date('Y-m-d', mktime(0,0,0,$month,$list_day,$year)), $plDates));
//var_dump(date('Y-m-d',mktime(0,0,0,$month,$list_day,$year)));
if (isset($dataByDate) && is_array($dataByDate))
{
if (in_array(date('Y-m-d', mktime(0,0,0,$month,$list_day,$year)), $dataByDate))
{
//$calendar.= $list_day;
}
}
$calendar.= '</td>'.PHP_EOL;
if($running_day == 6)
{
$calendar.= '</tr>'.PHP_EOL;
if(($days_counter + 1) != $days_in_month)
{
$calendar.= '<tr class="calendar-row">'.PHP_EOL;
}
$running_day = -1;
$days_in_this_week = 0;
}
$days_in_this_week++;
$running_day++;
$days_counter++;
}
/* finish the rest of the days in the week */
if($days_in_this_week < 8 && $running_day != 0)
{
for($x = 1; $x <= (8 - $days_in_this_week); $x++)
{
$calendar.= '<td class="calendar-day-np"> </td>'.PHP_EOL;
}
}
//final row
$calendar.= '</tr>'.PHP_EOL;
$calendar.= '</tbody>'.PHP_EOL;
$calendar.= '</table>'.PHP_EOL;
echo $calendar;
}