PHP開(kāi)發(fā)之製作簡(jiǎn)單日曆產(chǎn)生日曆的各個(gè)邊界值
自訂函數(shù)threshold方法,產(chǎn)生日曆的各個(gè)邊界值
#1)計(jì)算這個(gè)月總天數(shù)
2)計(jì)算這個(gè)月第一天與最後一天,各是星期幾
3)計(jì)算日曆中的第一個(gè)日期與最後一個(gè)日期
<?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); //上一個(gè)月最后一天 $lastMonthDate = strtotime('-1 day', $firstDay); $lastMonthOfLastDay = date('d', $lastMonthDate); //下一個(gè)月第一天 $nextMonthDate = strtotime('+1 day', $lastDay); $nextMonthOfFirstDay = strtotime('+1 day', $lastDay); //日歷的第一個(gè)日期 if($firstDayOfWeek == 7){ $firstDate = $firstDay; }else{ $firstDate = strtotime('-' . $firstDayOfWeek . ' day', $firstDay); } //日歷的最后一個(gè)日期 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 時(shí)間戳。
strtotime() 函數(shù)將任何英文文字的日期或時(shí)間描述解析為 Unix 時(shí)間戳記(自 January 1 1970 00:00:00 GMT 起的秒數(shù))。