Teach you to make a simple PHP calendar, make a simple PHP calendar _php tutorial

Source: Internet
Author: User
Tags php foreach

Teach you to make a simple PHP calendar, make a simple PHP calendar


In a recent project, you need to display the data in a calendar, there are many JS plug-ins on the Internet, in order to have greater control over their own, decided to make a calendar display. As shown in the following:

First, the calculation of data
1. New one Calendar class

2. Initialize data in two drop-down boxes, year and month

3. Initialize the year and month to search

4. Calculate data for each day of the calendar, 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//Get P The year data for the OST if (Empty ($_post[' ddlyear ')) {  $year = date (' Y ');} else {  $year = $_post[' ddlyear ');}//Get POST month data if (Empty ($_post[' ddlmonth ')) {  $month = date (' n ')} else {  $month = $_post[' Ddlmonth ');} $calendar = $util->threshold ($year, $month);//Get each boundary value $caculate = $util Caculate ($calendar);//Calculate calendar days with style $draws = $util->draw ($caculate);//Draw table, set TR and td?> in table

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

2, the Div do 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 is also ready, directly to the table printed out can
  

  <?php foreach ($years as $data) {?><?php if($year="=" $data)="" echo="" 'selected="selected" '?="">><?php echo $data?> <?php}?>
     <?php foreach ($months as $data) {?><?php if($month="=" $data)="" echo="" 'selected="selected" '?="">><?php echo $data?> <?php}?>
       
 
 
    <?php foreach ($draws as $draw) {?> 
   
     <?php foreach ($draw as $date) {?> 
    <?php}?> 
   <?php}?> 
  
Day One Two Three Four Five Six

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

Third, Calendar class
1. Threshold method, generate each boundary value of the calendar

1) Calculate the total number of days of the month

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

3) Calculate the first date in the calendar and the last date
  

/** * @deprecated generate each boundary value of the calendar * @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);  Number of days $days = Date ("T", $firstDay);  Gets the first day of the Week $firstDayOfWeek = Date ("N", $firstDay);    Get the last day of 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 ' and ' $lastDayOfWeek, ' lastmonthoflastday ' and $lastM Onthoflastday, ' firstdate ' and $firstDate, ' lastdate ' and $lastDate, ' year ' = $year, ' month ' and $mo NTH); }

2. Caculate method, calculate calendar days and styles

1) Calculate the number of days of the month, the first day of the month is not Sunday, it will need to be based on last month's final day

2) The number of days of the month will be traversed, if it is a rest day with a special CSS style

3) Calculate the number of days in the next month, in three cases, Sunday, Saturday and weekdays
  

/** * @author Pwstrick * @param array $calendar calculated data by threshold method * @deprecated calculates calendar days and styles */function caculate ($ca  Lendar) {$days = $calendar [' Days ']; $firstDayOfWeek = $calendar [' FirstDayOfWeek '];//week of the first day of the month $lastDayOfWeek = $calendar [' Lastdayofweek '];//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 Day of the month for ($i = 0; $i < $firstDayOfWeek; $i + +) {Array_push ($lastDays, $curren   t);//Add the date number of the last one months $current-; } $lastDays = Array_reverse ($lastDays);//reverse-order foreach ($lastDays as $index = $day) {Array_push ($dates, Array (' Da Y ' = $day, ' Tdclass ' ($index ==0? ')   Rest ': '), ' pclass ' = ' outter ');   }}//month calendar information for ($i = 1; $i <= $days; $i + +) {$isRest = $this->_checkisrest ($year, $month, $i); Determine if it is Array_push ($dates, Array (' Day ' = $i, ' tdclass ' = ($isRest? ')  Rest ': '), ' pclass ' = ');  }//Next month calendar information if ($lastDayOfWeek = = 7) {//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 table, set TR and TD in Table

1) The data will be displayed with the table tag, so the TD below each TR is arranged well

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

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

4) Add the middle row to the $TR, which is the array of each row

/**  * @author pwstrick  * @param array $caculate calculated data by 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);//Added to the returned data    $tr = Array ();//empty array list   }else {    array_push ($tr, $date);}  }  return $result;}

Through this article you should know the method of calendar production, then the strike, do a belong to their own calendar.

Attached Source: teach you to make a simple PHP calendar

http://www.bkjia.com/PHPjc/1072550.html www.bkjia.com true http://www.bkjia.com/PHPjc/1072550.html techarticle teach you to make a simple PHP calendar, make a simple PHP calendar of the recent project, you need to use the calendar to display the data, there are many JS plug-ins on the Internet, later for their own can have a larger ...

  • 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.