教大家制作簡單的php日歷
最近的一個項目中,需要將數(shù)據(jù)用日歷方式顯示,網(wǎng)上有很多的JS插件,后面為了自己能有更大的控制權(quán),決定自己制作一個日歷顯示。如下圖所示:

一、計算數(shù)據(jù)
1、new一個Calendar類
2、初始化兩個下拉框中的數(shù)據(jù),年份與月份
3、初始化要搜索的年份和月份
4、計算得出日歷中每一天的數(shù)據(jù)信息,包括css、天數(shù)
<?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);//月份數(shù)組
//獲取post的年份數(shù)據(jù)
if(empty($_POST['ddlYear'])) {
$year = date('Y');
}else {
$year = $_POST['ddlYear'];
}
//獲取post的月份數(shù)據(jù)
if(empty($_POST['ddlMonth'])) {
$month = date('n');
}else {
$month = $_POST['ddlMonth'];
}
$calendar = $util->threshold($year, $month);//獲取各個邊界值
$caculate = $util->caculate($calendar);//計算日歷的天數(shù)與樣式
$draws = $util->draw($caculate);//畫表格,設(shè)置table中的tr與td
?>
二、html展示
1、休息天的背景色是不同的,不是當(dāng)前搜索年月的天數(shù)字體顏色也是不同的
2、div中做初始化年份與月份的下拉框的操作,并選中當(dāng)前要搜索的年月
3、數(shù)據(jù)已計算好,哪個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)計算這個月總天數(shù)
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);
//取得天數(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
);
}
2、caculate方法,計算日歷的天數(shù)與樣式
1)將上個月的天數(shù)計算出來,本月第一天的星期不是星期天的話,就需要根據(jù)上個月的最后一天計算
2)將本月的天數(shù)遍歷出來,如果是休息天就加上特殊的css樣式
3)將下個月的天數(shù)計算出來,分三種情況,星期日、星期六和工作日
/**
* @author Pwstrick
* @param array $calendar 通過threshold方法計算后的數(shù)據(jù)
* @deprecated 計算日歷的天數(shù)與樣式
*/
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);//添加上一個月的日期天數(shù)
$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方法,畫表格,設(shè)置table中的tr與td
1)數(shù)據(jù)將要用table標(biāo)簽來顯示,所以這里要將各個tr下面的td排列好
2)$index % 7 == 0 計算表格每行的第一列
3)$index % 7 == 6 || $index == ($length-1) 計算每行的最后一列,或$caculate的最后一個數(shù)據(jù)
4)將中間行添加到$tr中,就是每一行的array
/**
* @author Pwstrick
* @param array $caculate 通過caculate方法計算后的數(shù)據(jù)
* @deprecated 畫表格,設(shè)置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);//添加到返回的數(shù)據(jù)中
$tr = array();//清空數(shù)組列表
}else {
array_push($tr, $date);
}
}
return $result;
}
通過本文大家應(yīng)該知道日歷制作的方法了,那就趁熱打鐵,做一個屬于自己日歷。
附源碼:教大家制作簡單的php日歷
相關(guān)文章
PHP 關(guān)于訪問控制的和運算符優(yōu)先級介紹
這篇文章主要介紹了PHP中關(guān)于訪問控制的和運算符優(yōu)先級介紹,需要的朋友可以參考下2013-07-07
Linux系統(tǒng)下php獲得系統(tǒng)分區(qū)信息的方法
這篇文章主要介紹了Linux系統(tǒng)下php獲得系統(tǒng)分區(qū)信息的方法,涉及Linux下php系統(tǒng)分析的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
PHP函數(shù)實現(xiàn)從一個文本字符串中提取關(guān)鍵字的方法
這篇文章主要介紹了PHP函數(shù)實現(xiàn)從一個文本字符串中提取關(guān)鍵字的方法,涉及php針對字符串的遍歷與查找等操作技巧,需要的朋友可以參考下2015-07-07
解析thinkphp import 文件內(nèi)容變量失效的問題
本篇文章是對thinkphp import文件內(nèi)容變量失效的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP連接SQLServer2005的實現(xiàn)方法(附ntwdblib.dll下載)
為了php連接sql2005 ,我在網(wǎng)絡(luò)上找了一大堆資料在我的csdn博客中.晚上3:05分時候終于搞定了2012-07-07

