PHP development to create a simple calendar to calculate the number of days and style of the calendar
Custom function caculate method to 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 Sunday, you need to calculate it based on the last day of the last month.
2) Calculate the number of days in this month The number of days is traversed, 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 day
<?php function caculate($calendar) { //$calendar 通过threshold方法计算后的数据 $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; } ?>
Note: The
array_push() function adds one or more elements to the end of the array of the first parameter and returns the length of the new array.
array_reverse() function returns an array in reverse order.