教大家製作簡單的php日曆,製作簡單php日曆_PHP教程

來源:互聯網
上載者:User

教大家製作簡單的php日曆,製作簡單php日曆


最近的一個項目中,需要將資料用日曆方式顯示,網上有很多的JS外掛程式,後面為了自己能有更大的控制權,決定自己製作一個日曆顯示。如所示:

一、計算資料
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列印出來即可
  

    <?php foreach($years as $data) {?>   ><?php echo $data?>  <?php }?>      <?php foreach($months as $data) {?>   ><?php echo $data?>  <?php }?>      
 
    <?php foreach($draws as $draw) {?> 
   
     <?php foreach($draw as $date) {?> 
     <?php }?> 
    <?php }?> 
  

<?php echo $date['day']?>

三、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; }

通過本文大家應該知道日曆製作的方法了,那就趁熱打鐵,做一個屬於自己日曆。

附源碼:教大家製作簡單的php日曆

http://www.bkjia.com/PHPjc/1072550.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1072550.htmlTechArticle教大家製作簡單的php日曆,製作簡單php日曆 最近的一個項目中,需要將資料用日曆方式顯示,網上有很多的JS外掛程式,後面為了自己能有更大...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.