上周我向您展示了如何创建 PHP 日历。这篇文章很受欢迎,所以我想在另一篇关于如何向日历添加控件的文章中跟进。毕竟,您不希望您的用户需要等到下个月才能看到当月以外的事件,对吧?现在我们将向日历添加“下个月”、“上个月”和月/年下拉控件。
PHP => HTML
/* 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).'" >Next Month >></a>'; /* "previous month" control */ $previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" ><< 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>'; echo $controls;
我不会解释代码,因为它既无聊又简单,但您可能想知道为什么我没有在 PHP 函数中包含控件。由于您可能需要也可能不需要这些控件,并且希望以不同的方式设置它们的样式,因此将控件放在 PHP 函数中并不是一个好主意。
您能想到要添加到日历的任何其他控件吗? AJAX 方法怎么样?分享它们!