PHP function to achieve a simple calendar

  1, this is a simple example php

  2, this example function is to achieve a simple calendar display.

  3, the role of this function is: enter the specified year, month, day, output current calendar month, and on a specified date entered, highlight.

  Specific code to achieve the following:

  

. 1 ? < PHP
 2  header ( "the Content-the Type: text / HTML; charset = UTF-8" );
 . 3  
. 4 date_default_timezone_set ( "a PRC" );
 . 5  
. 6  / * 
. 7  * Calendar function
 8   * / 
. 9   
10   / * *
 11    * output current year, month calendar
 12    * June 13, 2015
 13    * @author Gaoqing
 14    * @param int $ year in
 15    * @param int $ month the January
 16    * @param int $ current date Day
 17    * @return empty void
 18    * / 
19   function Calendar ( $ year, $ Month The , $ Day ) {
 20 is       / * 
21 is        *. 1, it determines the current year, month, how many days
 22        * 2, output of the basic frame of the calendar
 23        * 3, is determined on the first day of the current month, the week is
 24        * 4, all the days of the cycle, fill the calendar
 25        * / 
26       
27       // 1, determine the current year, month, how many days 
28       $ unix_time = mktime (0, 0, 0, $ month the , 1, $ year );
 29       / / $ mont_day = cal_days_in_month (CAL_GREGORIAN, month The $, $ year); 
30       $ mont_day = the intval ( DATE ( 'T', $ unix_time ));
31 is       
32       // 2, the output of the basic framework of the calendar 
33 is       echo "<Table border = '0' style = 'text-align = left: Center; width: 800px;'>" ;
 34 is       
35       echo "<TR>" ;
 36           echo "<th> Sun </ th>" ;
 37 [           echo "<th> Monday, </ th>" ;
 38 is           echo "<th> Tuesday, </ th>" ;
 39           echo "<th> Wednesday </ th>" ;
 40           echo "<TH> Thursday, </ TH>" ;
 41 is           echo "<TH> Friday </ TH>" ;
42 is           echo "<TH> Sat </ TH>" ;
 43 is       echo "</ TR>";
 44 is       
45       // 3, is determined on the first day of the current month, of the week 
46 is       $ first_day_week = the intval ( DATE ( 'W', $ unix_time ));
 47       
48       / * 
49        *. 4, all the days of the cycle, fill calendar
 50        * 4.1, first filling the first row, the first day of the week based on the number, fill in the blanks
 51        * 4.2, the number of successive cycles date, when $ i% 7, is described next week, the need for line
 52        * 4.3, after the cycle all the dates are determined whether to end of the table, if not the, with spaces
 53 is        * / 
54 is       echo "<TR>" ;
 55       // 4.1, first filling the first row, according to the first day of weeks, filled blank 
56 is       for ( $ I = 0; $ I <first_day_week $ ; $ I ++ ) {
 57 is           echo "<TD> & nbsp; </ TD>" ;
 58       }
 59       
60       // 4.2, the number of cycles in order date, when $ i% 7, described next week need wrap 
61 is       for ( $ J =. 1; $ J <= $ mont_day ; $ J ++ ) {
 62 is           IF ( $ J == $ Day ) {
 63 is               echo "<TD style = 'background: Red;'> { $ J } </ TD> " ;
 64           } the else {
 65               echo " <td>{J $ } </ TD> " ;
 66           }
 67           $ I ++ ;
 68           
69           IF ( $ I %. 7 == 0 ) {
 70               echo " </ TR> <TR> " ;
 71 is           }
 72       }
 73 is       
74       // 4.3, all the dates are circulating, it determines whether to end of the table, if not the, with spaces 
75       the while ( $ I %. 7 = 0! ) {
 76           $ I ++ ;
 77           echo "<TD> & nbsp; </ TD > " ;
 78       }
 79       
80       echo " </ TR> ";
81      
82      echo "</table>";
83  }
84  
85  calendar(2015, 6, 13);
86 ?>

  Simple page read:

  

Reproduced in: https: //www.cnblogs.com/gaoqing/articles/4573257.html

Guess you like

Origin blog.csdn.net/weixin_34060741/article/details/94261701