PHP開發(fā)之制作簡單日歷生成日歷的各個邊界值
自定義函數(shù)threshold方法,生成日歷的各個邊界值
1)計算這個月總天數(shù)
2)計算這個月第一天與最后一天,各是星期幾
3)計算日歷中的第一個日期與最后一個日期
<?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 ); } ?>
注釋:
mktime() 函數(shù)返回日期的 UNIX 時間戳。
strtotime() 函數(shù)將任何英文文本的日期或時間描述解析為 Unix 時間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù))。