教大家制作簡單的php日歷
最近的一個(gè)項(xiàng)目中,需要將數(shù)據(jù)用日歷方式顯示,網(wǎng)上有很多的JS插件,后面為了自己能有更大的控制權(quán),決定自己制作一個(gè)日歷顯示。如下圖所示:
一、計(jì)算數(shù)據(jù)
1、new一個(gè)Calendar類
2、初始化兩個(gè)下拉框中的數(shù)據(jù),年份與月份
3、初始化要搜索的年份和月份
4、計(jì)算得出日歷中每一天的數(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);//獲取各個(gè)邊界值 $caculate = $util->caculate($calendar);//計(jì)算日歷的天數(shù)與樣式 $draws = $util->draw($caculate);//畫表格,設(shè)置table中的tr與td ?>
二、html展示
1、休息天的背景色是不同的,不是當(dāng)前搜索年月的天數(shù)字體顏色也是不同的
2、div中做初始化年份與月份的下拉框的操作,并選中當(dāng)前要搜索的年月
3、數(shù)據(jù)已計(jì)算好,哪個(gè)td屬于哪個(gè)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方法,生成日歷的各個(gè)邊界值
1)計(jì)算這個(gè)月總天數(shù)
2)計(jì)算這個(gè)月第一天與最后一天,各是星期幾
3)計(jì)算日歷中的第一個(gè)日期與最后一個(gè)日期
/** * @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 ); }
2、caculate方法,計(jì)算日歷的天數(shù)與樣式
1)將上個(gè)月的天數(shù)計(jì)算出來,本月第一天的星期不是星期天的話,就需要根據(jù)上個(gè)月的最后一天計(jì)算
2)將本月的天數(shù)遍歷出來,如果是休息天就加上特殊的css樣式
3)將下個(gè)月的天數(shù)計(jì)算出來,分三種情況,星期日、星期六和工作日
/** * @author Pwstrick * @param array $calendar 通過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; }
3、draw方法,畫表格,設(shè)置table中的tr與td
1)數(shù)據(jù)將要用table標(biāo)簽來顯示,所以這里要將各個(gè)tr下面的td排列好
2)$index % 7 == 0 計(jì)算表格每行的第一列
3)$index % 7 == 6 || $index == ($length-1) 計(jì)算每行的最后一列,或$caculate的最后一個(gè)數(shù)據(jù)
4)將中間行添加到$tr中,就是每一行的array
/** * @author Pwstrick * @param array $caculate 通過caculate方法計(jì)算后的數(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)該知道日歷制作的方法了,那就趁熱打鐵,做一個(gè)屬于自己日歷。
附源碼:教大家制作簡單的php日歷
相關(guān)文章
php中文亂碼怎么辦如何讓瀏覽器自動(dòng)識(shí)別utf-8
調(diào)試php的時(shí)候經(jīng)常顯示亂碼,每次打開瀏覽器都要改下編碼,下面為大家介紹個(gè)不錯(cuò)的方法讓瀏覽器自動(dòng)識(shí)別為utf-8,有類似情況的額朋友可以參考下2014-01-01PHP中使用imagick實(shí)現(xiàn)把PDF轉(zhuǎn)成圖片
這篇文章主要介紹了PHP中使用imagick實(shí)現(xiàn)把PDF轉(zhuǎn)成圖片,本文著重講解了2個(gè)錯(cuò)誤的處理,特別是GhostScript錯(cuò)誤,需要的朋友可以參考下2015-01-01PHP實(shí)現(xiàn)表單提交時(shí)去除斜杠的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)表單提交時(shí)去除斜杠的方法,涉及php針對頁面表單提交元素過濾操作的相關(guān)技巧,需要的朋友可以參考下2016-12-12PHP基于curl后臺(tái)遠(yuǎn)程登錄正方教務(wù)系統(tǒng)的方法
這篇文章主要介紹了PHP基于curl后臺(tái)遠(yuǎn)程登錄正方教務(wù)系統(tǒng)的方法,結(jié)合實(shí)例形式分析了php使用curl及cookie實(shí)現(xiàn)遠(yuǎn)程登陸的操作技巧,需要的朋友可以參考下2016-10-10PHP用mysql_insert_id()函數(shù)獲得剛插入數(shù)據(jù)或當(dāng)前發(fā)布文章的ID
向mysql 插入數(shù)據(jù)時(shí),很多時(shí)候我們想知道剛剛插入數(shù)據(jù)的id,這對我們很有用。下面這篇文章就詳細(xì)給大家介紹了利用mysql_insert_id()函數(shù)獲得剛插入數(shù)據(jù)或當(dāng)前發(fā)布文章的ID,有需要的朋友們可以參考借鑒,感興趣的朋友們下面來一起看看吧。2016-11-11PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能
這篇文章主要介紹了PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能,結(jié)合實(shí)例形式分析了php基于memcache的session共享相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04js和php郵箱地址驗(yàn)證的實(shí)現(xiàn)方法
這篇文章主要介紹了js和php郵箱地址驗(yàn)證的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01php中 ob_start等函數(shù)截取標(biāo)準(zhǔn)輸出的方法
這篇文章主要介紹了php中 ob_start等函數(shù)截取標(biāo)準(zhǔn)輸出的方法的相關(guān)資料,需要的朋友可以參考下2015-06-06