Teach you how to make a simple php calendar_php skills
In a recent project, the data needed to be displayed in a calendar. There are many JS plug-ins on the Internet. Later, in order to have greater control, I decided to make a calendar display myself. As shown below:
1. Calculated data
1. Create a new Calendar class
2. Initialize the data in the two drop-down boxes, 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 number of days
<?php require_once 'calendar.php'; $util = new Calendar(); $years = array(2012, 2013, 2014, 2015, 2016);//年份选择自定义 $months = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//月份数组 //获取post的年份数据 if(empty($_POST['ddlYear'])) { $year = date('Y'); }else { $year = $_POST['ddlYear']; } //获取post的月份数据 if(empty($_POST['ddlMonth'])) { $month = date('n'); }else { $month = $_POST['ddlMonth']; } $calendar = $util->threshold($year, $month);//获取各个边界值 $caculate = $util->caculate($calendar);//计算日历的天数与样式 $draws = $util->draw($caculate);//画表格,设置table中的tr与td ?>
2. HTML display
1. The background color of rest days is different, and the font color of days other than the current search year and month is also different
2. Initialize the year and month drop-down boxes in the div, and select the year and month currently to be searched
3. The data has been calculated and which td belongs to which tr. You can just print out the table
<div style="padding:20px"> <select name="ddlYear"> <?php foreach($years as $data) {?> <option value="<?php echo $data?>" <?php if($year == $data) echo 'selected="selected"'?>><?php echo $data?></option> <?php }?> </select> <select name="ddlMonth"> <?php foreach($months as $data) {?> <option value="<?php echo $data?>" <?php if($month == $data) echo 'selected="selected"'?>><?php echo $data?></option> <?php }?> </select> <input type="submit" value="修改"/> </div> <table width="100%" cellspacing="0" class="table_calendar"> <thead class="f14"> <tr> <td width="16%">日</td> <td width="14%">一</td> <td width="14%">二</td> <td width="14%">三</td> <td width="14%">四</td> <td width="14%">五</td> <td width="14%">六</td> </tr> </thead> <tbody class="f14"> <?php foreach($draws as $draw) {?> <tr> <?php foreach($draw as $date) {?> <td class="<?php echo $date['tdclass']?>"> <p class="<?php echo $date['pclass']?>"><?php echo $date['day']?></p> </td> <?php }?> </tr> <?php }?> </tbody> </table>
3. Calendar class
1. Threshold method, generates each boundary value of the calendar
1) Calculate the total number of days in this month
2) Calculate the first day and last day of this month, and what day of the week each is
3) Calculate the first date and the last date in the calendar
/** * @deprecated 生成日历的各个边界值 * @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); //取得天数 $days = date("t", $firstDay); //取得第一天是星期几 $firstDayOfWeek = date("N", $firstDay); //获得最后一天是星期几 $lastDayOfWeek = date('N', $lastDay); //上一个月最后一天 $lastMonthDate = strtotime('-1 day', $firstDay); $lastMonthOfLastDay = date('d', $lastMonthDate); //下一个月第一天 $nextMonthDate = strtotime('+1 day', $lastDay); $nextMonthOfFirstDay = strtotime('+1 day', $lastDay); //日历的第一个日期 if($firstDayOfWeek == 7) $firstDate = $firstDay; else $firstDate = strtotime('-'. $firstDayOfWeek .' day', $firstDay); //日历的最后一个日期 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 number of days and style of the calendar
1) Calculate the number of days in the last month. If the first day of the month is not a Sunday, you need to calculate it based on the last day of the last month
2) Traverse the number of days in this month, and if it is a rest day, add a special css style
3) Calculate the number of days in the next month, divided into three situations, Sunday, Saturday and working days
/** * @author Pwstrick * @param array $calendar 通过threshold方法计算后的数据 * @deprecated 计算日历的天数与样式 */ function caculate($calendar) { $days = $calendar['days']; $firstDayOfWeek = $calendar['firstDayOfWeek'];//本月第一天的星期 $lastDayOfWeek = $calendar['lastDayOfWeek'];//本月最后一天的星期 $lastMonthOfLastDay = $calendar['lastMonthOfLastDay'];//上个月的最后一天 $year = $calendar['year']; $month = $calendar['month']; $dates = array(); if($firstDayOfWeek != 7) { $lastDays = array(); $current = $lastMonthOfLastDay;//上个月的最后一天 for ($i = 0; $i < $firstDayOfWeek; $i++) { array_push($lastDays, $current);//添加上一个月的日期天数 $current--; } $lastDays = array_reverse($lastDays);//反序 foreach ($lastDays as $index => $day) { array_push($dates, array('day' => $day, 'tdclass' => ($index ==0 ?'rest':''), 'pclass' => 'outter')); } } //本月日历信息 for ($i = 1; $i <= $days; $i++) { $isRest = $this->_checkIsRest($year, $month, $i); //判断是否是休息天 array_push($dates, array('day' => $i, 'tdclass' => ($isRest ?'rest':''), 'pclass' => '')); } //下月日历信息 if($lastDayOfWeek == 7) {//最后一天是星期日 $length = 6; } elseif($lastDayOfWeek == 6) {//最后一天是星期六 $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 the table, set tr and td in the table
1) The data will be displayed using table tags, so the td under each tr must be arranged
2)$index % 7 == 0 Calculate the first column of each row of 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 通过caculate方法计算后的数据 * @deprecated 画表格,设置table中的tr与td */ function draw($caculate) { $tr = array(); $length = count($caculate); $result = array(); foreach ($caculate as $index => $date) { if($index % 7 == 0) {//第一列 $tr = array($date); }elseif($index % 7 == 6 || $index == ($length-1)) { array_push($tr, $date); array_push($result, $tr);//添加到返回的数据中 $tr = array();//清空数组列表 }else { array_push($tr, $date); } } return $result; }
Through this article, you should know how to make a calendar, so strike while the iron is hot and make a calendar of your own.
Source code attached: Teach you how to make a simple php calendar

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
