Examples of calendar functions implemented based on ThinkPHP, thinkphp calendar
This example describes the Calendar function based on ThinkPHP. We will share this with you for your reference. The details are as follows:
Development Environment
A simple calendar is developed to collect statistics about the work. To facilitate development, use the ThinkPHP architecture. Interface Example
Note: Each page contains the date of the previous month, the current month, and the next month, which are distinguished by different colors. If you work one day, it will be circled.
Mainly the following two files
Important file description
Function File
CalenDar. class. php is mainly responsible for obtaining CalenDar details and does not involve user data operations.
The Code is as follows:
<? Phpnamespace Util; class CalenDar {// previous month information private $ lastYear = null; private $ lastMonth = null; // current month information private $ curYear = null; private $ curMonth = null; private $ curWek = null; private $ curDay = null; private $ curDaySum = 0; // The information of the next month is private $ nextYear = null; // which year is the next month private $ nextMonth = null; // The next month private $ calendar = null; public function _ construct ($ dateTime = null) {if (isset ($ _ get ['yeal']) & is_numeric ($ _ get ['Yeal']) {$ this-> curYear = $ _ get ['yeal'];} else {$ this-> curYear = date ('y ');} if (isset ($ _ get ['month']) & is_numeric ($ _ get ['month']) {$ this-> curMonth = $ _ get ['month'];} else {$ this-> curMonth = date ('n ');} if (isset ($ _ get ['day']) & is_numeric ($ _ get ['day']) {$ this-> curDay =$ _ get ['day'];} else {$ this-> curDay = date ('J ');} $ this-> init ($ dateTime); $ this-> createCalendar ();}/*** initialize */public function init ($ DateTime = null) {if (! Empty ($ dateTime) {// the current month $ this-> curYear = date ('y', strtotime ($ dateTime )); $ this-> curMonth = date ('n', strtotime ($ dateTime); $ this-> curDay = date ('J', strtotime ($ dateTime ));} $ this-> curWek = date ('w', strtotime ($ this-> curYear. '-'. $ this-> curMonth. '-1'); // previous month $ this-> lastMonth = $ this-> curMonth-1; // last month $ this-> lastYear = $ this-> curYear; // The year of the previous month if ($ this-> lastMonth <0) {$ this-> lastMonth = 12; $ this-> lastYear-= 1;} // The next month $ this-> nextMonth = $ this-> curMonth + 1; // next month $ this-> nextYear = $ this-> curYear; // the year of the next month if ($ this-> nextMonth> 12) {$ this-> nextMonth = 1; $ this-> nextYear + = 1 ;}} public function getCalendar () {return $ this-> calendar ;} /*** create a calendar from Sunday, Monday, Wednesday, Thursday, Saturday, 7*5 squares. Fill in the last day of the previous month and the last day of the next month. ***/public function createCalendar () {// determine the total number of days in the current month $ nextStr = $ this-> nextYear. '-'. $ this-> nextMonth. '-1-1 days'; $ curDaySum = date ('J', strtotime ($ nextStr )); // determine the number of the last day of the previous month $ lastStr = $ this-> curYear. '-'. $ this-> curMonth. '-1-1 days'; $ lastDaySum = date ('J', strtotime ($ lastStr); $ prefixLId = $ this-> lastYear. '-'. $ this-> lastMonth; $ prefixCId = $ this-> curYear. '-'. $ this-> curMonth; $ prefixNId = $ this-> nextYear. '-'. $ this-> nextMonth; if ($ this-> curWek = 0) {$ lastMonthSum = 7; // Add $ lastMonthSum day of last month} else {$ lastMonthSum = $ this-> curWek;} $ lastMonthStart = $ lastDaySum-$ lastMonthSum + 1; for ($ I = 0, $ j = 1, $ k = 1; $ I <42; $ I ++) {$ dateInfo = array (); if ($ I <$ lastMonthSum) {// last month $ dateInfo ['day'] = $ lastMonthStart + $ I; $ dateInfo ['type'] = 1; $ id = $ prefixLId. '-'. $ dateInfo ['day']; $ this-> calendar [] = array ('id' => $ id, 'info' => $ dateInfo );} else if ($ j> $ curDaySum) {// next month $ id = $ prefixNId. '-'. $ k; $ dateInfo ['day'] = $ k; $ dateInfo ['type'] = 3; $ this-> calendar [] = array ('id' => $ id, 'info' => $ dateInfo); $ k ++ ;} else {// This month $ dateInfo ['day'] = $ j; $ dateInfo ['type'] = 2; $ this-> calendar [] = array ('id' => ($ prefixCId. '-'. $ j), 'info' => $ dateInfo); $ j ++; $ this-> curDaySum + = 1 ;}} public function getDayTime () {return $ this-> curYear. '-'. $ this-> curMonth. '-'. $ this-> curDay;}/*** get the month of the current month **/public function getCurMonth () {return $ this-> curYear. '-'. $ this-> curMonth;}/*** get the month of the previous month **/public function getLastMonth () {return $ this-> lastYear. '-'. $ this-> lastMonth;}/*** get the month of the next month **/public function getNextMonth () {return $ this-> nextYear. '-'. $ this-> nextMonth;}/*** determine the number of days of the current month **/public function getCurDaySum () {return $ this-> curDaySum ;}}
The WorkLog. class. php file is mainly used to combine user work information with calendar information.
<? Phpnamespace Util; class WorkLog {private $ uid; // user ID private $ calendar; // calendar Object private $ lastMonth; // last month private $ curMonth; // private $ nextMonth of the current month; // private $ monthWorkedDays of the next month; // private $ workLog = null for working days of the current month; // The current month's work calendar public function _ construct ($ uid = 0, $ daytime = null) {$ this-> uid = $ uid; $ this-> calendar = new \ Util \ CalenDar ($ daytime); $ this-> lastMonth = $ this-> calendar-> getLastMonth (); $ this-> curMonth = $ This-> calendar-> getCurMonth (); $ this-> nextMonth = $ this-> calendar-> getNextMonth (); $ this-> init ();} public function init () {$ info = $ this-> calendar-> getCalendar (); $ userLog = array (); if ($ this-> uid! = 0) {$ lastMonth = explode ('-', $ this-> lastMonth); $ curMonth = explode ('-', $ this-> curMonth ); $ nextMonth = explode ('-', $ this-> nextMonth); // obtain the log of the previous month, current month, and next month, use cache later $ where = 'uid = '. $ this-> uid. AND ('Year' = '. $ lastMonth [0]. AND 'month' = '. $ lastMonth [1]. ') or ('Year' = '. $ curMonth [0]. AND 'month' = '. $ curMonth [1]. ') or ('Year' = '. $ nextMonth [0]. AND 'month' = '. $ nextMonth [1]. ')'; $ rs = M ('work _ log')-> field ('year, month, day, status')-> where ($ where) -> select (); foreach ($ rs as $ value) {$ userLog [$ value ['Year']. '-'. $ value ['month']. '-'. $ value ['day'] = $ value ['status']; }}$ flag = 1; foreach ($ info as $ key => $ value) {if ($ flag % 7 = 1 | $ flag % 7 = 0) {$ cellbgtype = 3; // No work} else {$ cellbgtype = 1; // No work} if (isset ($ userLog [$ value ['id']) & $ userLog [$ value ['id'] = 1) {// determine whether the current month is $ str = date ('Y-n', strtotime ($ value ['id']); if ($ this-> curMonth ==$ str) {$ this-> monthWorkedDays + = 1 ;}$ cellbgtype + = 1 ;}$ cellbgtype = 'daytype '. $ cellbgtype. '_'. $ value ['info'] ['type']; $ info [$ key] ['info'] ['class'] = $ cellbgtype; $ flag ++ ;} $ this-> workLog = $ info;} public function getLastMonth () {return $ this-> lastMonth;} public function getCurMonth () {return $ this-> curMonth ;} public function getNextMonth () {return $ this-> nextMonth;}/*** Number of days that have been working in the current month */public function monthWorkedDays () {return $ this-> monthWorkedDays ;} /*** number of days in the current month **/public function monthDays () {return $ this-> calendar-> getCurDaySum ();} /*** get the accumulated working days **/public function workedDays () {}/ *** current calendar month **/public function logMonth () {}/*** current salary as of the current month **/public function hadSalary () {}/*** estimated maximum salary **/public function maxSalary () {}/ *** current calendar of work **/public function workInfo () {return $ this-> workLog ;}}
Call File
IndexController. class. php
<? Phpnamespace Home \ Controller; use Think \ Controller; class IndexController extends Controller {public function index () {$ uid = 1; $ daytime = I ('daytime'); if (! Empty ($ daytime) {$ daytime = date ('Y-m-d', $ daytime);} $ WorkLog = new \ Util \ WorkLog ($ uid, $ daytime); $ curDay = $ WorkLog-> getCurMonth (); $ lastMDay = strtotime ($ curDay. '-1 month'); $ nextMDay = strtotime ($ curDay. '+ 1 month'); $ nowTime = date ('current time: Y, m, d, H: I: s'); $ curDayStr = date ('calendar time: y-year m-d ', strtotime ($ curDay); $ info = $ WorkLog-> workInfo (); $ curDaySum = $ WorkLog-> monthDays (); $ workDays = $ WorkLog-> monthWorkedDays (); $ this-> assign ('lastworkday', $ lastdayday); $ this-> assign ('nextday', $ nextMDay ); $ this-> assign ('nowtime', $ nowTime); $ this-> assign ('curday', $ curDayStr); $ this-> assign ('info ', $ info); $ this-> assign ('curdaysum', $ curDaySum); $ this-> assign ('workdays ', $ workDays ); $ this-> display ();}}
Show files
Index.html
<Extend name = "Public/base"/> <block name = "css"> <style type = "text/css"> * {margin: 0; padding: 0; border: 0; font-size: 16px;} body {background-color: #4d56a3;} ul {list-style: none ;}# mainBox {width: 1024px; height: auto; margin: 10px auto 0 auto;} # leftBox {width: 60%; height: 700px; float: left; border-radius: 15px; margin-right: 3%; background-color: # ffbd66 ;}# rightBox {width: 37%; height: 700px; float: r Ight; border-radius: 15px; background-color: # faf7dd} # calendartitle {width: 95%; margin: 10px auto 5px auto; height: 110px ;} # calendartitle ul li {float: left; margin-right: 10px ;}# logoImg {width: 100px; height: 100px; border-radius: 10px; background-image: url ('_ IMG _/logo.png'); background-repeat: no-repeat ;}# cellHead {width: 95%; background-color: # ffe786; border-radius: 10px 10px 0 0;} # cellHead Td {width: 80px; height: 45px; font-size: 22px ;}# calendarcell {width: 95%; margin: 0 auto ;}# calendarTable td {width: 80px; height: 80px; background-repeat: no-repeat; border: 1px; font-size: 18px ;}. daytype1_1 {background-image: url ("_ IMG _/cellbg1_0_1.png ");}. daytype2_1 {background-image: url ("_ IMG _/cellbg1_1_1.png ");}. daytype1_2 {background-image: url ("_ IMG _/cellbg1_0_2.png ");}. daytype2_2 {backgrou Nd-image: url ("_ IMG _/cellbg1_1_2.png ");}. daytype1_3 {background-image: url ("_ IMG _/cellbg1_0_3.png ");}. daytype2_3 {background-image: url ("_ IMG _/cellbg1_1_3.png ");}. daytype3_1 {background-image: url ("_ IMG _/cellbg2_0_1.png ");}. daytype4_1 {background-image: url ("_ IMG _/cellbg2_00001.png ");}. daytype3_2 {background-image: url ("_ IMG _/cellbg2_0_2.png ");}. daytype4_2 {background-image: url ("_ IMG _/cellbg 2_1_2.png ");}. daytype3_3 {background-image: url ("_ IMG _/cellbg2_0_3.png ");}. daytype4_3 {background-image: url ("_ IMG _/cellbg2_20.3.png ");}. aBlock {display: block; text-decoration: none ;}. ainblock {display: inline-block; text-decoration: none ;}. actionBox {width: 80%; margin: 0 auto; margin-top: 15px; background-color: # fadfbb; height: auto; padding-top: 20px; padding-bottom: 20px; border-radius: 10px; Text-align: center ;}# action1 a {width: 80%; margin: 0 auto 15px auto; height: 45px; background-color: # ffec42; border-radius: 10px; line-height: 45px; text-align: center; font-size: 22px; color: # ad5408; font-weight: 700;} # action1 a: hover {font-size: 24px; color: # F12 ;}# action2 {text-align: center ;}# action2 a {width: 80px; background-color: # ffec42; border-radius: 10px; height: 35px; line-height: 3 5px; text-align: center; margin-right: 10px ;}# action3 {text-align: left ;}# action3 ul li {margin-left: 20px; border-bottom: 1px dashed #999; margin-right: 10px; margin-bottom: 10px; font-size: 20px ;} </style> </block> <block name = "title"> calendar </block> <block name = "main"> <div id = "mainBox"> <div id = 'leftbox'> <div id = "calendartitle"> <ul> <li id = "logoImg"> </li> <div style = "height: 45px; line-height: 45px; Font-size: 20px; ">{$ nowTime} </div> <div style =" height: 45px; line-height: 45px; font-size: 20px; ">{$ curDay }</div> </li> </ul> </div> <div id =" calendarcell "> <div id =" cellHead "> <table cellpadding = "0" cellspacing = "0" border = "0" align = "center"> <tr> <td align = "center"> Sunday </td> <td align = "center"> Monday </td> <td align = "center"> Tuesday </td> <td align = "center"> Wednesday </td> <td align = "center"> Thursday </td> <td align = "center"> Friday </td> <td align = "center"> Saturday </td> </tr> </table> </div> <table cellpadding = "0" cellspacing =" 0 "border =" 0 "id = 'calendartable' align =" center "> <tr> <volist name =" info "id =" dayinfo "key = 'flag'> <td align = "center" class = "{$ dayinfo ['info'] ['class']}" >{$ dayinfo ['info'] ['day']} </td> <if condition = "$ flag % 7 = 0 & $ flag % 7! = 42 "> </tr> </if> </volist> </table> </div> <div id = 'rightbox'> <div id = "action1" class = "actionBox"> <ul> <li> <a href = "javascript :; "rel =" external nofollow "class =" aBlock "onclick =" todayAction (1) "> Work </a> </li> <a href =" javascript :; "rel =" external nofollow "class =" aBlock "onclick =" todayAct Ion (2) "> rest </a> </li> </ul> </div> <div id =" action2 "class =" actionBox "> <ul> <li>
Train of Thought Analysis
1. In CalenDar. class. php, encapsulate the date information of each month. If you need a calendar, you only need to call the file as a class.
For example
$ Calendar = new \ Util \ CalenDar (); $ info = $ calendar-> getCalendar (); echo json_encode ($ info); // The output result is as follows: [{"id ": "", "info": {"day": 29, "type": 1 }}, {"id": "", "info ": {"day": 30, "type": 1 }}, {"id": "2016-5-31", "info": {"day": 31, "type ": 1 }}, {"id": "2016-6-1", "info": {"day": 1, "type": 2 }}, {"id ": "2016-6-2", "info": {"day": 2, "type": 2 }}, {"id": "2016-6-3", "info ": {"day": 3, "type": 2 }}, {"id": "2016-6-4", "info": {"day": 4, "type ": 2 }}, {"id": "2016-6-5", "info": {"day": 5, "type": 2 }}, {"id ": "2016-6-6", "info": {"day": 6, "type": 2 }}, {"id": "2016-6-7", "info ": {"day": 7, "type": 2 }}, {"id": "2016-6-8", "info": {"day": 8, "type ": 2 }}, {"id": "2016-6-9", "info": {"day": 9, "type": 2 }}, {"id ": "2016-6-10", "info": {"day": 10, "type": 2 }}, {"id": "2016-6-11", "info ": {"day": 11, "type": 2 }}, {"id": "2016-6-12", "info": {"day": 12, "type ": 2 }}, {"id": "2016-6-13", "info": {"day": 13, "type": 2 }}, {"id ": "2016-6-14", "info": {"day": 14, "type": 2 }}, {"id": "2016-6-15", "info ": {"day": 15, "type": 2 }}, {"id": "2016-6-16", "info": {"day": 16, "type ": 2 }}, {"id": "2016-6-17", "info": {"day": 17, "type": 2 }}, {"id ": "2016-6-18", "info": {"day": 18, "type": 2 }}, {"id": "2016-6-19", "info ": {"day": 19, "type": 2 }}, {"id": "2016-6-20", "info": {"day": 20, "type ": 2 },{ "id": "2016-6-21", "info": {"day": 21, "type": 2 }}, {"id ": "2016-6-22", "info": {"day": 22, "type": 2 }}, {"id": "2016-6-23", "info ": {"day": 23, "type": 2 }}, {"id": "2016-6-24", "info": {"day": 24, "type ": 2 },{ "id": "2016-6-25", "info": {"day": 25, "type": 2 }}, {"id ": "2016-6-26", "info": {"day": 26, "type": 2 }}, {"id": "2016-6-27", "info ": {"day": 27, "type": 2 }}, {"id": "2016-6-28", "info": {"day": 28, "type ": 2 }}, {"id": "2016-6-29", "info": {"day": 29, "type": 2 }}, {"id ": "2016-6-30", "info": {"day": 30, "type": 2 }}, {"id": "2016-7-1", "info ": {"day": 1, "type": 3 }}, {"id": "2016-7-2", "info": {"day": 2, "type ": 3 },{ "id": "2016-7-3", "info": {"day": 3, "type": 3 }}, {"id ": "2016-7-4", "info": {"day": 4, "type": 3 }}, {"id": "2016-7-5", "info ": {"day": 5, "type": 3 }}, {"id": "2016-7-6", "info": {"day": 6, "type ": 3 },{ "id": "2016-7-7", "info": {"day": 7, "type": 3 }}, {"id ": "2016-7-8", "info": {"day": 8, "type": 3 }}, {"id": "2016-7-9", "info ": {"day": 9, "type": 3}]
2. in WorkLog. class. in php, to get the user's work information for the previous month, the current month, and the next month, the reason why the use of a one-time get three months of work information, because if the day to read, in this way, the number of data queries is too large. Of course, it is better to cache the data. After reading the work information, you can use the calendar to determine whether the day is working and whether it is a weekend to determine the background style of each square in the calendar. Job information databases such:
PS: Here are some online date tools for your reference:
Online calendar:
Http://tools.jb51.net/bianmin/wannianli
Web calendar:
Http://tools.jb51.net/bianmin/webwannianli
Online calendar flash version:
Http://tools.jb51.net/bianmin/flashwnl