カレンダーのさまざまな境界値を生成するシンプルなカレンダーを作成する PHP 開発
関數(shù)のしきい値メソッドをカスタマイズして、カレンダーの各境界値を生成します
1) 今月の合計(jì)日數(shù)を計(jì)算します
2) 今月の各曜日の最初と最後の日を計(jì)算します
3) カレンダーを計(jì)算します 最初の日付と最後の日付
<?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 タイムスタンプを返します。
strtotime() 関數(shù)は、英語(yǔ)テキストの日付または時(shí)刻の説明を Unix タイムスタンプ (1970 年 1 月 1 日 00:00:00 GMT からの秒數(shù)) に解析します。