PHP 自制日歷
一、計算數據
1、new一個Calendar類
2、初始化兩個下拉框中的數據,年份與月份
3、初始化要搜索的年份和月份
4、計算得出日歷中每一天的數據信息,包括css、天數
復制代碼
<?php
require_once 'calendar.php';
$util = new Calendar();
$years = array(2012, 2013, 2014, 2015, 2016);//年份選擇自定義
$months = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//月份數組
//獲取post的年份數據
if(empty($_POST['ddlYear'])) {
$year = date('Y');
}else {
$year = $_POST['ddlYear'];
}
//獲取post的月份數據
if(empty($_POST['ddlMonth'])) {
$month = date('n');
}else {
$month = $_POST['ddlMonth'];
}
$calendar = $util->threshold($year, $month);//獲取各個邊界值
$caculate = $util->caculate($calendar);//計算日歷的天數與樣式
$draws = $util->draw($caculate);//畫表格,設置table中的tr與td
?>
復制代碼
二、html展示
1、休息天的背景色是不同的,不是當前搜索年月的天數字體顏色也是不同的
2、div中做初始化年份與月份的下拉框的操作,並選中當前要搜索的年月
3、數據已計算好,哪個td屬於哪個tr也已做好,直接將table打印出來即可
復制代碼
<div style="padding:20px">
<select name="ddlYear">
<?php foreach($years as $data) {?>
<option value="<?php echo $data?>" <?php if($year == $data) echo 'selected="selected"'?>><?php echo $data?></option>
<?php }?>
</select>
<select name="ddlMonth">
<?php foreach($months as $data) {?>
<option value="<?php echo $data?>" <?php if($month == $data) echo 'selected="selected"'?>><?php echo $data?></option>
<?php }?>
</select>
<input type="submit" value="修改"/>
</div>
<table width="100%" cellspacing="0" class="table_calendar">
<thead class="f14">
<tr>
<td width="16%">日</td>
<td width="14%">一</td>
<td width="14%">二</td>
<td width="14%">三</td>
<td width="14%">四</td>
<td width="14%">五</td>
<td width="14%">六</td>
</tr>
</thead>
<tbody class="f14">
<?php foreach($draws as $draw) {?>
<tr>
<?php foreach($draw as $date) {?>
<td class="<?php echo $date['tdclass']?>">
<p class="<?php echo $date['pclass']?>"><?php echo $date['day']?></p>
</td>
<?php }?>
</tr>
<?php }?>
</tbody>
</table>
復制代碼
三、Calendar類
1、threshold方法,生成日歷的各個邊界值
1)計算這個月總天數
2)計算這個月第一天與最後一天,各是星期幾
3)計算日歷中的第一個日期與最後一個日期
復制代碼
/**
* @deprecated 生成日歷的各個邊界值
* @param string $year
* @param string $month
* @return array
*/
function threshold($year, $month) {
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$lastDay = strtotime('+1 month -1 day', $firstDay);
//取得天數
$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
);
}
復制代碼
2、caculate方法,計算日歷的天數與樣式
1)將上個月的天數計算出來,本月第一天的星期不是星期天的話,就需要根據上個月的最後一天計算
2)將本月的天數遍歷出來,如果是休息天就加上特殊的css樣式
3)將下個月的天數計算出來,分三種情況,星期日、星期六和工作日
復制代碼
/**
* @author Pwstrick
* @param array $calendar 通過threshold方法計算後的數據
* @deprecated 計算日歷的天數與樣式
*/
function caculate($calendar) {
$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;
}
復制代碼
3、draw方法,畫表格,設置table中的tr與td
1)數據將要用table標簽來顯示,所以這裡要將各個tr下面的td排列好
2)$index % 7 == 0 計算表格每行的第一列
3)$index % 7 == 6 || $index == ($length-1) 計算每行的最後一列,或$caculate的最後一個數據
4)將中間行添加到$tr中,就是每一行的array
復制代碼
/**
* @author Pwstrick
* @param array $caculate 通過caculate方法計算後的數據
* @deprecated 畫表格,設置table中的tr與td
*/
function draw($caculate) {
$tr = array();
$length = count($caculate);
$result = array();
foreach ($caculate as $index => $date) {
if($index % 7 == 0) {//第一列
$tr = array($date);
}elseif($index % 7 == 6 || $index == ($length-1)) {
array_push($tr, $date);
array_push($result, $tr);//添加到返回的數據中
$tr = array();//清空數組列表
}else {
array_push($tr, $date);
}
}
return $result;
}