Home > Article > Backend Development > Teach you how to make a simple php calendar, make a simple php calendar_PHP tutorial
In a recent project, the data needs to be displayed in calendar mode. There are many JS plug-ins on the Internet. In order to You can have greater control and decide to create a calendar display yourself. As shown below:
1. Calculated data
1. Create a new Calendar class
2. Initialize the data in the two drop-down boxes, year and month
3. Initialize the year and month to be searched
4. Calculate the data information of each day in the calendar, including css and number of days
<?php require_once 'calendar.php'; $util = new Calendar(); $years = array(2012, 2013, 2014, 2015, 2016);//年份选择自定义 $months = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//月份数组 //获取post的年份数据 if(empty($_POST['ddlYear'])) { $year = date('Y'); }else { $year = $_POST['ddlYear']; } //获取post的月份数据 if(empty($_POST['ddlMonth'])) { $month = date('n'); }else { $month = $_POST['ddlMonth']; } $calendar = $util->threshold($year, $month);//获取各个边界值 $caculate = $util->caculate($calendar);//计算日历的天数与样式 $draws = $util->draw($caculate);//画表格,设置table中的tr与td ?>
2. HTML display
1. The background color of rest days is different, and the font color of days other than the current search year and month is also different
2. Initialize the year and month drop-down boxes in the div, and select the year and month currently to be searched
3. The data has been calculated and which td belongs to which tr. You can just print out the table
<div style="padding:20px"> <select name="ddlYear"> <?php foreach($years as $data) {?> <option value="<?php echo $data?>" <?php if($year == $data) echo 'selected="selected"'?>><?php echo $data?></option> <?php }?> </select> <select name="ddlMonth"> <?php foreach($months as $data) {?> <option value="<?php echo $data?>" <?php if($month == $data) echo 'selected="selected"'?>><?php echo $data?></option> <?php }?> </select> <input type="submit" value="修改"/> </div> <table width="100%" cellspacing="0" class="table_calendar"> <thead class="f14"> <tr> <td width="16%">日</td> <td width="14%">一</td> <td width="14%">二</td> <td width="14%">三</td> <td width="14%">四</td> <td width="14%">五</td> <td width="14%">六</td> </tr> </thead> <tbody class="f14"> <?php foreach($draws as $draw) {?> <tr> <?php foreach($draw as $date) {?> <td class="<?php echo $date['tdclass']?>"> <p class="<?php echo $date['pclass']?>"><?php echo $date['day']?></p> </td> <?php }?> </tr> <?php }?> </tbody> </table>
3. Calendar class
1. Threshold method, generates each boundary value of the calendar
1) Calculate the total number of days in this month
2) Calculate the first day and last day of this month, and what day of the week each is
3) Calculate the first date and the last date in the calendar
/** * @deprecated 生成日历的各个边界值 * @param string $year * @param string $month * @return array */ function threshold($year, $month) { $firstDay = mktime(0, 0, 0, $month, 1, $year); $lastDay = strtotime('+1 month -1 day', $firstDay); //取得天数 $days = date("t", $firstDay); //取得第一天是星期几 $firstDayOfWeek = date("N", $firstDay); //获得最后一天是星期几 $lastDayOfWeek = date('N', $lastDay); //上一个月最后一天 $lastMonthDate = strtotime('-1 day', $firstDay); $lastMonthOfLastDay = date('d', $lastMonthDate); //下一个月第一天 $nextMonthDate = strtotime('+1 day', $lastDay); $nextMonthOfFirstDay = strtotime('+1 day', $lastDay); //日历的第一个日期 if($firstDayOfWeek == 7) $firstDate = $firstDay; else $firstDate = strtotime('-'. $firstDayOfWeek .' day', $firstDay); //日历的最后一个日期 if($lastDayOfWeek == 6) $lastDate = $lastDay; elseif($lastDayOfWeek == 7) $lastDate = strtotime('+6 day', $lastDay); else $lastDate = strtotime('+'.(6-$lastDayOfWeek).' day', $lastDay); return array( 'days' => $days, 'firstDayOfWeek' => $firstDayOfWeek, 'lastDayOfWeek' => $lastDayOfWeek, 'lastMonthOfLastDay' => $lastMonthOfLastDay, 'firstDate' => $firstDate, 'lastDate' => $lastDate, 'year' => $year, 'month' => $month ); }
2. caculate method, calculate the number of days and style of the calendar
1) Calculate the number of days in the last month. If the first day of the month is not a Sunday, you need to calculate it based on the last day of the last month
2) Traverse the number of days in this month, and if it is a rest day, add a special css style
3) Calculate the number of days in the next month, divided into three situations, Sunday, Saturday and working days
/** * @author Pwstrick * @param array $calendar 通过threshold方法计算后的数据 * @deprecated 计算日历的天数与样式 */ function caculate($calendar) { $days = $calendar['days']; $firstDayOfWeek = $calendar['firstDayOfWeek'];//本月第一天的星期 $lastDayOfWeek = $calendar['lastDayOfWeek'];//本月最后一天的星期 $lastMonthOfLastDay = $calendar['lastMonthOfLastDay'];//上个月的最后一天 $year = $calendar['year']; $month = $calendar['month']; $dates = array(); if($firstDayOfWeek != 7) { $lastDays = array(); $current = $lastMonthOfLastDay;//上个月的最后一天 for ($i = 0; $i < $firstDayOfWeek; $i++) { array_push($lastDays, $current);//添加上一个月的日期天数 $current--; } $lastDays = array_reverse($lastDays);//反序 foreach ($lastDays as $index => $day) { array_push($dates, array('day' => $day, 'tdclass' => ($index ==0 ?'rest':''), 'pclass' => 'outter')); } } //本月日历信息 for ($i = 1; $i <= $days; $i++) { $isRest = $this->_checkIsRest($year, $month, $i); //判断是否是休息天 array_push($dates, array('day' => $i, 'tdclass' => ($isRest ?'rest':''), 'pclass' => '')); } //下月日历信息 if($lastDayOfWeek == 7) {//最后一天是星期日 $length = 6; } elseif($lastDayOfWeek == 6) {//最后一天是星期六 $length = 0; }else { $length = 6 - $lastDayOfWeek; } for ($i = 1; $i <= $length; $i++) { array_push($dates, array('day' => $i, 'tdclass' => ($i==$length ?'rest':''), 'pclass' => 'outter')); } return $dates; }
3. Draw method, draw the table, set tr and td in the table
1) The data will be displayed using table tags, so the td under each tr must be arranged
2)$index % 7 == 0 Calculate the first column of each row of the table
3)$index % 7 == 6 || $index == ($length-1) Calculate the last column of each row, or the last data of $caculate
4) Add the middle row to $tr, which is the array of each row
/** * @author Pwstrick * @param array $caculate 通过caculate方法计算后的数据 * @deprecated 画表格,设置table中的tr与td */ function draw($caculate) { $tr = array(); $length = count($caculate); $result = array(); foreach ($caculate as $index => $date) { if($index % 7 == 0) {//第一列 $tr = array($date); }elseif($index % 7 == 6 || $index == ($length-1)) { array_push($tr, $date); array_push($result, $tr);//添加到返回的数据中 $tr = array();//清空数组列表 }else { array_push($tr, $date); } } return $result; }
Through this article, you should know how to make a calendar, so strike while the iron is hot and make a calendar of your own.
Source code attached: teach you how to make a simple php calendar