Teach you to make simple PHP calendar _php tips

Source: Internet
Author: User
Tags deprecated php foreach

A recent project, the need to use the calendar to display the data, there are many JS Plug-ins on the web, the back of their own to have greater control over the decision to make a calendar display. As shown in the following illustration:

I. Calculation of data
1, new one calendar class

2. Initialize data in two dropdown boxes, year and month

3, initialize the year and month to search

4, calculate the calendar every day of data information, including CSS, days

<?php
 require_once ' calendar.php ';
 $util = new Calendar ();
 $years = Array (2012, 2013, 2014, 2015, 2016);/year Select Custom $months = Array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
 );//month array 
   //gets the year data for the POST
 if (Empty ($_post[' ddlyear ')) {
  $year = date (' Y ');
 } else {
  $year = $_post[' ddlyear '];
 }
 Gets the month data for POST
 if (Empty ($_post[' ddlmonth ')) {
  $month = date (' n ');
 } else {
  $month = $_post[' Ddlmonth '];
 }

 $calendar = $util->threshold ($year, $month);//Get each boundary value
 $caculate = $util->caculate ($calendar); Calculates the number of days and styles for a calendar
 $draws = $util->draw ($caculate);//Draw a table, set the TR and TD?> in table


Second, the HTML display
1, rest day background color is different, not the number of days of the current search font color is also different

2, the div in the initialization of the year and month Drop-down box operation, and select the current date to search

3, the data has been calculated well, which TD belongs to which TR has been done, directly to the table can be printed
  

<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= "Modify"/> </di v> <table width= "100%" cellspacing= "0" class= "Table_calendar" > <thead class= "F14" > <tr> & LT;TD width= "16%" > Day </td> <td width= "14%" > One </td> <td width= "14%" > Two </td> TD width= "14%" > Three </td> <td width= "14%" > Four </td> <td width= "14%" > Five </td> <t D width= "14%" > Six </td> </tr> </thead> <tbody class= "F14" > <?php foreach ($draws as $draw) {?> &LT;TR&G
    T <?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>

Third, Calendar class
1, Threshold method, generate the calendar of the various boundary values

1) Calculate the total number of days this month

2) Calculate the first and last days of the month, the day of the week

3 Calculate the first date and last date in the calendar
  

/** * @deprecated generate each boundary value of the calendar * @param string $year * @param string $month * @return Array/function Threshol
  D ($year, $month) {$firstDay = mktime (0, 0, 0, $month, 1, $year);
  $lastDay = Strtotime (' +1 month-1 Day ', $firstDay);
  Gets the number of days $days = Date ("T", $firstDay);
  Get the first day of the Week $firstDayOfWeek = Date ("N", $firstDay);
  
  Get the last day is the week $lastDayOfWeek = Date (' N ', $lastDay);
  Last one months $lastMonthDate = Strtotime ('-1 day ', $firstDay);
  $lastMonthOfLastDay = Date (' d ', $lastMonthDate);
  The first day of the next one months $nextMonthDate = Strtotime (' +1 days ', $lastDay);
  
  $nextMonthOfFirstDay = Strtotime (' +1 Day ', $lastDay);
  The first date of the calendar if ($firstDayOfWeek = = 7) $firstDate = $firstDay;
  else $firstDate = strtotime ('-'. $firstDayOfWeek. ' Day ', $firstDay);
  The last date of the calendar 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 ' => $lastDayOfWe
    Ek, ' lastmonthoflastday ' => $lastMonthOfLastDay, ' firstdate ' => $firstDate, ' lastdate ' => $lastDate,
 ' Year ' => $year, ' month ' => $month);
 }

2, the Caculate method, calculates the calendar the number of days and the style

1 to calculate the number of days of the last month, the first day of the week is not Sunday, it needs to be based on the last day of last month

2 The number of days of the month to traverse out, if it is a day off to add a special CSS style

3 The number of days in the next month is calculated, divided into three cases, Sunday, Saturday and working days
  

/** * @author Pwstrick * @param array $calendar The data calculated by the Threshold method * @deprecated calculate the number of days and styles for the calendar/function Cacula
  Te ($calendar) {$days = $calendar [' Days ']; $firstDayOfWeek = $calendar [' FirstDayOfWeek '];//week of the first day of the month $lastDayOfWeek = $calendar [' Lastdayofweek '];//week of the last day of the month $
  Lastmonthoflastday = $calendar [' Lastmonthoflastday '];//the last day of last month $year = $calendar [' Year '];
  
  $month = $calendar [' Month '];
  $dates = Array ();
   if ($firstDayOfWeek!= 7) {$lastDays = array (); $current = $lastMonthOfLastDay Last last day for ($i = 0; $i < $firstDayOfWeek; $i + +) {Array_push ($lastDays, $curr
   ENT)//Add one month date days $current-; $lastDays = Array_reverse ($lastDays);//reverse-order foreach ($lastDays as $index => $day) {Array_push ($dates, Array ( ' Day ' => $day, ' Tdclass ' => ($index ==0?
   Rest ': '), ' pclass ' => ' outter ');
   }//month calendar information for ($i = 1; $i <= $days; $i + +) {$isRest = $this->_checkisrest ($year, $month, $i); Judge whether it is the day of rest array_pUsh ($dates, Array (' Day ' => $i, ' Tdclass ' => ($isRest? ')
  Rest ': '), ' Pclass ' => ');
  //Next month calendar information if ($lastDayOfWeek = = 7) {//The last day is Sunday $length = 6;
  ElseIf ($lastDayOfWeek = = 6) {//Last day is Saturday $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 method, draw a table, set table in the TR and TD

1 The data will be displayed with the table label, so here's a list of the TD below each TR.

2) $index% 7 = 0 Calculating the first column in each row of the table

3) $index% 7 = 6 | | $index = = ($length-1) calculates the last column of each row, or the last data of the $caculate

4 The middle row is added to the $TR, which is the array of each row

/**
  * @author pwstrick
  * @param array $caculate The data calculated by the Caculate method
  * @deprecated draw a table, set the TR and TD in Table * *
 function Draw ($caculate) {
  $tr = array ();
  $length = count ($caculate);
  $result = Array ();
  foreach ($caculate as $index => $date) {
   if ($index% 7 = 0) {//first column
    $tr = array ($date);
   } ElseIf ($index% 7 = 6 | | $index = = ($length-1)) {
    Array_push ($tr, $date);
    Array_push ($result, $TR);//Add to returned data
    $tr = Array ();//empty array list
   }else {
    array_push ($tr, $date);
   }
  } return
  $result;
 }

Through this article we should know the way to make the calendar, then the iron strike, do a belong to their own calendar.

Source code: Teach you to make a simple PHP calendar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.