PHP Homemade Calendar _php Tutorial

Source: Internet
Author: User
Tags deprecated php foreach

PHP Homemade Calendar


First, calculate data 1, new A Calendar Class 2, initialize two drop-down boxes of data, year and month 3, initialize the year and month to search for, and calculate the data information for each day of the calendar, including CSS, the number of days to copy code Threshold ($year, $month);//Get each boundary value $caculate = $util->caculate ($calendar);//Calculate calendar days with style $draws = $util->draw ($ caculate);//Draw the table, set the table in the TR and td?> copy code two, HTML display 1, the background color of the rest day is different, not the current search years of the days of the font color is also different 2, the Div do the initialization of the year and the Month drop-down box operation, and select the current date to search 3, the data has been calculated, which TD belongs to which TR is also done, directly print the table can be copied code > > TD Width= "14%" > a !--? php foreach ($draws as $draw) {?--> !--? php foreach ($draw as $date) {?-->
Day two three four five six
Copy code three, Calendar Class 1, Threshold method, generate the calendar of each boundary value 1) Calculate the total number of days of the Month 2) calculate the first day of the month and the last day, each week a few 3) calculate the first date in the calendar and the last date copy code/** * @depreca Ted generates various boundary values for the calendar * @param string $year * @param string $month * @return Array */function threshold ($year, $month) {$firstD ay = 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 ' = $lastDayOfWeek, ' Lastmont Hoflastday ' + $lastMonthOfLastDay, ' firstdate ' = $firstDate, ' lastdate ' + $lastDate, ' year ' and $year, ' Mont h ' = $month); } Copy Code 2, Caculate method, calculate calendar days and style 1) the number of days of the month is calculated, the first day of the month is not Sunday, you need to calculate the last day of last month 2) will be the number of days of the month to walk out, if the rest of the day with a special CSS style 3) Calculate the number of days in the next month, in three cases, Sunday, Saturday, and weekday copy code/** * @author Pwstrick * @param array $calendar calculated data by threshold method * @deprecated Calculated Calendar days and Styles */function caculate ($calendar) {$days = $calendar [' days ']; $firstDayOfWeek = $calendar [' FirstDayOfWeek '];// The 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 last month for ($i = 0; $i < $firstDayOf Week; $i + +) {Array_push ($lastDaYS, $current);//Add date days for the last one months $current-; } $lastDays = Array_reverse ($lastDays);//reverse-order foreach ($lastDays as $index = $day) {Array_push ($dates, Array (' Day ' =&gt ; $day, ' tdclass ' = ($index ==0? ') Rest ': '), ' pclass ' = ' outter '); }}//month calendar information for ($i = 1; $i <= $days; $i + +) {$isRest = $this->_checkisrest ($year, $month, $i);//Determine whether it is a day of rest Array_pu SH ($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, ' TD Class ' = = ($i = = $length? ') Rest ': '), ' pclass ' = ' outter '); } return $dates; } Copy Code 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 of the table 3) $index% 7 = 6 | | $index = = ($length-1) calculates the last column of each row, or the last data of $caculate 4) adds the middle row to the $TR, which is the array copy code for each row/** * @author PwStrick * @param array $caculate calculated data by caculate method * @deprecated draw a table, set the TR and TD */function Draw ($caculate) {$tr = ar in table Ray (); $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 = Arra Y ();//empty array list}else {Array_push ($tr, $date);}} return $result; }

http://www.bkjia.com/PHPjc/885672.html www.bkjia.com true http://www.bkjia.com/PHPjc/885672.html techarticle PHP Homemade Calendar One, calculate data 1, new A Calendar Class 2, initialize two drop-down boxes of data, year and month 3, initialize the year to search and month 4, calculate the calendar ...

  • Related Article

    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.