我试图从http://davidwalsh.name/php-calendar-controls设置日历。他提供了一些很棒的代码,而且在纸面上看起来似乎非常适合我需要的东西。但我有点生锈与我的PHP和我得到一些错误。这是一篇古老的博客文章,所以我想我可能会在这里得到更快的回复。继承人代码:
<?php
/* draws a calendar */
function draw_calendar($month,$year){
/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$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;
}
/* date settings */
$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y'));
/* select month control */
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';
/* select year control */
$year_range = 7;
$select_year_control = '<select name="year" id="year">';
for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
$select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';
/* "next month" control */
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month >></a>';
/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='. ($month != 1 ? $year : $year - 1).'" class="control"><< Previous Month</a>';
/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.' <input type="submit" name="submit" value="Go" /> '.$previous_month_link.' '.$next_month_link.' </form>';
?>
首次加载日历时出现错误:注意:未定义的索引:第70行的C:\ wamp \ www \ david walsh calendar \ draw_calendar.php和第71行的年份。
我尝试过这样的事情:
if(!isset($month)){
$month = date(‘n’);
} else{
$month = (int) ($_GET['month'] ? $_GET['month'] : date(‘m’));
}
但是它没有工作它加载正确但然后日历按钮停止工作转到下个月或上个月。无论如何要在当前月份和年份加载日历并仍允许下一个和上个月的按钮工作?
事先感谢任何可以解决问题的人!
答案 0 :(得分:0)
将其添加到代码的末尾:
echo $controls; echo draw_calendar($month,$year);
答案 1 :(得分:0)
将 70 和 71 更改为:
/* date settings */
$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));
$year = (int) (isset($_GET['year']) ? $_GET['year'] : date('Y'));
并在文件末尾添加:
echo $controls;
echo draw_calendar($month,$year);
答案 2 :(得分:0)
<?php
echo $mth[$mo]." ".$yr;
?>
如果不起作用,请参阅此以获取更多帮助: http://www.epctutorials.com/