《PHP學(xué)習(xí):教大家制作簡(jiǎn)單的php日歷》要點(diǎn):
本文介紹了PHP學(xué)習(xí):教大家制作簡(jiǎn)單的php日歷,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP應(yīng)用最近的一個(gè)項(xiàng)目中,需要將數(shù)據(jù)用日歷方式顯示,網(wǎng)上有很多的JS插件,后面為了本身能有更大的控制權(quán),決定本身制作一個(gè)日歷顯示.如下圖所示:
PHP應(yīng)用
PHP應(yīng)用一、計(jì)算數(shù)據(jù)
1、new一個(gè)Calendar類(lèi)
PHP應(yīng)用2、初始化兩個(gè)下拉框中的數(shù)據(jù),年份與月份
PHP應(yīng)用3、初始化要搜索的年份和月份
PHP應(yīng)用4、計(jì)算得出日歷中每一天的數(shù)據(jù)信息,包含css、天數(shù)
PHP應(yīng)用
<?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);//獲取各個(gè)邊界值
$caculate = $util->caculate($calendar);//計(jì)算日歷的天數(shù)與樣式
$draws = $util->draw($caculate);//畫(huà)表格,設(shè)置table中的tr與td
?>
PHP應(yīng)用二、html展示
1、休息天的配景色是不同的,不是當(dāng)前搜索年月的天數(shù)字體顏色也是不同的
PHP應(yīng)用2、div中做初始化年份與月份的下拉框的操作,并選中當(dāng)前要搜索的年月
PHP應(yīng)用3、數(shù)據(jù)已計(jì)算好,哪個(gè)td屬于哪個(gè)tr也已做好,直接將table打印出來(lái)即可
PHP應(yīng)用
<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>
PHP應(yīng)用三、Calendar類(lèi)
1、threshold辦法,生成日歷的各個(gè)邊界值
PHP應(yīng)用 1)計(jì)算這個(gè)月總天數(shù)
PHP應(yīng)用 2)計(jì)算這個(gè)月第一天與最后一天,各是星期幾
PHP應(yīng)用 3)計(jì)算日歷中的第一個(gè)日期與最后一個(gè)日期
PHP應(yīng)用
/**
* @deprecated 生成日歷的各個(gè)邊界值
* @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);
//上一個(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
);
}
PHP應(yīng)用2、caculate辦法,計(jì)算日歷的天數(shù)與樣式
PHP應(yīng)用 1)將上個(gè)月的天數(shù)計(jì)算出來(lái),本月第一天的星期不是星期天的話(huà),就必要根據(jù)上個(gè)月的最后一天計(jì)算
PHP應(yīng)用 2)將本月的天數(shù)遍歷出來(lái),如果是休息天就加上特殊的css樣式
PHP應(yīng)用 3)將下個(gè)月的天數(shù)計(jì)算出來(lái),分三種情況,星期日、星期六和工作日
PHP應(yīng)用
/**
* @author Pwstrick
* @param array $calendar 通過(guò)threshold辦法計(jì)算后的數(shù)據(jù)
* @deprecated 計(jì)算日歷的天數(shù)與樣式
*/
function caculate($calendar) {
$days = $calendar['days'];
$firstDayOfWeek = $calendar['firstDayOfWeek'];//本月第一天的星期
$lastDayOfWeek = $calendar['lastDayOfWeek'];//本月最后一天的星期
$lastMonthOfLastDay = $calendar['lastMonthOfLastDay'];//上個(gè)月的最后一天
$year = $calendar['year'];
$month = $calendar['month'];
$dates = array();
if($firstDayOfWeek != 7) {
$lastDays = array();
$current = $lastMonthOfLastDay;//上個(gè)月的最后一天
for ($i = 0; $i < $firstDayOfWeek; $i++) {
array_push($lastDays, $current);//添加上一個(gè)月的日期天數(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;
}
PHP應(yīng)用3、draw辦法,畫(huà)表格,設(shè)置table中的tr與td
PHP應(yīng)用 1)數(shù)據(jù)將要用table標(biāo)簽來(lái)顯示,所以這里要將各個(gè)tr下面的td排列好
PHP應(yīng)用 2)$index % 7 == 0 計(jì)算表格每行的第一列
PHP應(yīng)用 3)$index % 7 == 6 || $index == ($length-1) 計(jì)算每行的最后一列,或$caculate的最后一個(gè)數(shù)據(jù)
PHP應(yīng)用 4)將中間行添加到$tr中,便是每一行的array
PHP應(yīng)用
/**
* @author Pwstrick
* @param array $caculate 通過(guò)caculate辦法計(jì)算后的數(shù)據(jù)
* @deprecated 畫(huà)表格,設(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;
}
PHP應(yīng)用通過(guò)本文大家應(yīng)該知道日歷制作的辦法了,那就趁熱打鐵,做一個(gè)屬于自己日歷.
PHP應(yīng)用附源碼:教年夜家制作簡(jiǎn)單的php日歷
歡迎參與《PHP學(xué)習(xí):教大家制作簡(jiǎn)單的php日歷》討論,分享您的想法,維易PHP學(xué)院為您提供專(zhuān)業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/8389.html