PHP development to create a simple calendar to generate each boundary value of the calendar
Custom function threshold method to generate each boundary value of the calendar
1) Calculate the total number of days in this month
2) Calculate the first day of this month day and last day, each is a day of the week
3) Calculate the first date and last date in the calendar
<?php function threshold($year, $month) { $firstDay = mktime(0, 0, 0, $month, 1, $year); $lastDay = strtotime('+1 month -1 day', $firstDay); //取得天數(shù) $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 ); } ?>
Note:
mktime() function returns the date UNIX timestamp.
The strtotime() function parses any English text date or time description into a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT).