How to create a simple php calendar

Source: Internet
Author: User
Tags php foreach
We will teach you how to create a simple php calendar, a php calendar dedicated to you. do not miss out on your favorite calendar. In a recent project, you need to display data in calendar mode. There are many JS plug-ins on the internet. later, you decided to create a calendar display for yourself to gain greater control. As shown in:

I. Computing data
1. a new Calendar class

2. initialize the data in the two drop-down lists, year and month.

3. initialize the year and month to be searched.

4. calculate the data information of each day in the calendar, including css and days.

<? Php require_once 'Calendar. php '; $ util = new Calendar (); $ years = array (2012,201 3, 2014,201 5, 2016); // select custom year $ months = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ); // month array // Obtain the year data of post if (empty ($ _ POST ['ddlyear ']) {$ year = date ('Y ');} else {$ year = $ _ POST ['ddlyear '];} // Obtain the month data of post if (empty ($ _ POST ['ddlmonth']) {$ month = date ('N');} else {$ month = $ _ POST ['ddlmonth'];} $ calendar = $ util-> th Reshold ($ year, $ month); // Obtain each boundary value $ caculate = $ util-> caculate ($ calendar ); // calculate the calendar's days and style $ draws = $ util-> draw ($ caculate); // draw a table and set tr and td in the table?>

II. html display
1. the background color of the rest day is different, and the font color is not the number of days in the current search year or month.

2. initialize the year and month drop-down boxes in p and select the year to be searched.

3. The data has been calculated, and which td belongs to which tr has been prepared. print the table directly.
  

<? 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 }?>
Day I II 3. Thu V. Sat.

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

III. Calendar class
1. the threshold method generates the border values of the calendar.

1) calculate the total number of days of the month

2) calculate the day of the week and the last day of the month.

3) calculate the first and last dates in the calendar
  

/*** @ Deprecated generates the border values 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 ); // get the number of days $ days = date ("t", $ firstDay); // get the first day of the week $ firstDayOfWeek = date ("N", $ firstDay ); // Obtain the last day of the week $ lastDayOfWeek = date ('N', $ lastDay); // The last day of the previous month $ lastMonthDate = strtotime ('-1 Day ', $ firstDay); $ lastMonthOfLastDay = date ('D', $ lastMonthDate); // The first day of the next month $ nextMonthDate = strtotime ('+ 1 Day', $ 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, 'lastmonthoflastday' => $ lastMonthOfLastDay, 'firstdate' => $ firstDate, 'lastdate' => $ lastDate, 'year' => $ year, 'month' => $ month );}

2. caculate method: calculate the calendar days and style

1) calculate the number of days in the previous month. if the day of the first day of this month is not Sunday, it is calculated based on the last day of the previous month.

2) traverse the days of the month. if it is a day off, add a special css style.

3) calculate the number of days of the next month in three cases: Sunday, Saturday, and Workday.
  

/*** @ Author Pwstrick * @ param array $ calendar data calculated using the threshold method * @ deprecated calculate the calendar days and style */function caculate ($ calendar) {$ days = $ calendar ['Days']; $ firstDayOfWeek = $ calendar ['firstdayofweek ']; // The Day of the week on the first day of the month $ lastDayOfWeek = $ calendar ['lastdayfweek'] // the week of the last day of the month $ lastMonthOfLastDay = $ calendar ['lastmonthoflastday']; // The last day of the last month $ year = $ calendar ['Year']; $ month = $ calendar ['month']; $ dates = arra Y (); if ($ firstDayOfWeek! = 7) {$ lastDays = array (); $ current = $ lastMonthOfLastDay; // The last day of the last month for ($ I = 0; $ I <$ firstDayOfWeek; $ I ++) {array_push ($ lastDays, $ current); // add the date days of the previous month $ current -- ;}$ lastDays = array_reverse ($ lastDays ); // reverse foreach ($ lastDays as $ index => $ day) {array_push ($ dates, array ('day' => $ day, 'tdclass' => ($ index = 0? 'Rest': ''), 'pclass '=> 'outter');} // calendar information of the current month for ($ I = 1; $ I <= $ days; $ I ++) {$ isRest = $ this-> _ checkIsRest ($ year, $ month, $ I); // checks whether the array_push ($ dates, array ('day' => $ I, 'tdclass' => ($ isRest? 'Rest': ''), 'pclass '=>'');} // calendar information of the next month if ($ lastDayOfWeek = 7) {// The last day is Sunday $ length = 6;} elseif ($ lastDayOfWeek = 6) {// The 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 a table using the draw method and set tr and td in the table

1) The data will be displayed using the table label, so we need to arrange the td under each tr here

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

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

4) add the middle row to $ tr, which is the array of each row.

/*** @ Author Pwstrick * @ param array $ caculate the data calculated using the caculate method * @ deprecated draws a table and sets tr and td */function draw ($ caculate) in the table) {$ tr = array (); $ length = count ($ caculate); $ result = array (); foreach ($ caculate as $ index => $ date) {if ($ index % 7 = 0) {// The first column $ tr = array ($ date );} elseif ($ index % 7 = 6 | $ index = ($ length-1) {array_push ($ tr, $ date); array_push ($ result, $ tr); // add to the returned data $ tr = array (); // clear the array List} else {array_push ($ tr, $ date );}} return $ result ;}

After reading this article, you should know how to create a calendar.

Source code: to create a simple php 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.