Table of Contents
PHP practice: Calendar (simple code, easy to expand)
Home Backend Development PHP Tutorial PHP Practice: Calendar (Simple Code, Easy to Expand)_PHP Tutorial

PHP Practice: Calendar (Simple Code, Easy to Expand)_PHP Tutorial

Jul 13, 2016 am 10:22 AM
code calendar

PHP practice: Calendar (simple code, easy to expand)

I took the time to write a calendar program, focusing only on functions and implementation ideas, so the code and functions are relatively simple, but it is also easier to understand and expand.

The show() function is used to display the calendar. You can modify the show() function to display different years and months by passing values.

<?php
    class Calendar{
        public $weekarray = array(&#39;星期日&#39;,&#39;星期一&#39;,&#39;星期二&#39;,&#39;星期三&#39;,&#39;星期四&#39;,&#39;星期五&#39;,&#39;星期六&#39;);
        public $firstDay = &#39;&#39;;//当月第一天
        public $firstNum = &#39;&#39;;//返回当月第一天对应的星期数字
        public $firstDayNum = &#39;&#39;;//当月第一天对应的中文星期
        
        /**
        *   指定日是星期几
        *   eg:$date=&#39;2014-07-21&#39;
        */
        public function getWeek($date){
            $date = strtotime($date);//strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳
            $num = date(&#39;w&#39;,$date);//数字型的星期几,如: "0" (星期日) 至 "6" (星期六)
            return $num;
        }

        /**
        *   取得指定月有多少天
        *   eg:$month = &#39;2014-07&#39;
        */
        public function getMonthNum($month){
            $month = strtotime($month);
            return date(&#39;t&#39;,$month);//指定月份的天数
        }
        
        /**
        *   显示日历
        */
        public function show(){
            //取得当前日期
            $year = date(&#39;Y&#39;);
            $month = date(&#39;m&#39;);
            $day = date(&#39;d&#39;);
            echo &#39;<table border="1" style="text-align:center;">&#39;;
            echo &#39;<tr><td colspan="7">&#39;.$year.&#39;-&#39;.$month.&#39;</td></tr>&#39;;
            echo &#39;<tr><th>星期日</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></tr>&#39;;
            //取得当前月有多少天
            $yearMonth = &#39;$year."-".$month&#39;;
            $monthNum = $this->getMonthNum($yearMonth);
            //取得当前月第一天是星期几
            $this->firstDay = $year."-".$month."-01";
            $this->firstNum = $this->getWeek($this->firstDay);
            $this->firstDayNum = $this->weekarray[$this->firstNum];
            
            echo &#39;<tr>&#39;;
                for($i=1;$i<=$monthNum+($this->firstNum);$i++){
                    echo &#39;<td>&#39;;
                    if($i >= $this->firstNum+1){
                        $a = $i-$this->firstNum;
                        if($a == $day){
                            echo &#39;<div style="background-color:blue;color:#fff;">&#39;.$a.&#39;</div>&#39;;
                        }else{
                            echo &#39;<div>&#39;.$a.&#39;</div>&#39;;
                        }
                    }
                    echo &#39;</td>&#39;;
                    if($i%7 == 0){//每输出7列就换一行
                        echo &#39;</tr>&#39;;
                    }
                }
            echo &#39;</table>&#39;;
        }

    }
    
    $calendar = new Calendar();
    $calendar->show();
Copy after login

Rendering:

PHP Practice: Calendar (Simple Code, Easy to Expand)_PHP TutorialUsage of PHP date() function

Here is a detailed explanation of the date() function: http://wenku.baidu.com/link?url=OPDGzaCMWgjDE0ya8QlDbLIXX0c11ohUjsoLyRm-cYp7lz-O_7H4XBILv8JfomhbnSxXSW0FhqxYBK0_gn8Nr77XMWp-_st7v8AYecbNZjG

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847853.htmlTechArticlePHP practice: Calendar (simple code, easy to expand) I took the time to write a calendar program, focusing only on functions and implementation. ideas, so the code and functions are relatively simple, but understanding and expansion are also easier than...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if the win11 dual-screen calendar does not exist on the second monitor? What should I do if the win11 dual-screen calendar does not exist on the second monitor? Jun 12, 2024 pm 05:47 PM

An important tool for organizing your daily work and routine in Windows 11 is the display of time and date in the taskbar. This feature is usually located in the lower right corner of the screen and gives you instant access to the time and date. By clicking this area, you can bring up your calendar, making it easier to check upcoming appointments and dates without having to open a separate app. However, if you use multiple monitors, you may run into issues with this feature. Specifically, while the clock and date appear on the taskbar on all connected monitors, the ability to click the date and time on a second monitor to display the calendar is unavailable. As of now, this feature only works on the main display - it's unlike Windows 10, where clicking on any

How to solve win7 driver code 28 How to solve win7 driver code 28 Dec 30, 2023 pm 11:55 PM

Some users encountered errors when installing the device, prompting error code 28. In fact, this is mainly due to the driver. We only need to solve the problem of win7 driver code 28. Let’s take a look at what should be done. Do it. What to do with win7 driver code 28: First, we need to click on the start menu in the lower left corner of the screen. Then, find and click the "Control Panel" option in the pop-up menu. This option is usually located at or near the bottom of the menu. After clicking, the system will automatically open the control panel interface. In the control panel, we can perform various system settings and management operations. This is the first step in the nostalgia cleaning level, I hope it helps. Then we need to proceed and enter the system and

Win10 calendar displays week numbers Win10 calendar displays week numbers Jan 04, 2024 am 08:41 AM

Many users want to use the win10 calendar tool to check the current number of days, but the calendar does not automatically display this function. In fact, we only need to make simple settings to see the cumulative number of weeks this year ~ win10 calendar displays weeks Digital setting tutorial: 1. Enter calendar in the search in the lower left corner of the desktop and open the application. 2. In the open calendar application, click the "gear" icon in the lower left corner, and the settings will pop up on the right. We click "Calendar Settings" 3. Continue in the open calendar settings, find "Week Number" and then change the week Just adjust the number option to "the first day of the year". 4. After completing the above settings, click "Week" to see this year's week number statistics.

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

Outlook calendar not syncing; Outlook calendar not syncing; Mar 26, 2024 am 09:36 AM

If your Outlook calendar cannot sync with Google Calendar, Teams, iPhone, Android, Zoom, Office account, etc., please follow the steps below to resolve the issue. The calendar app can be connected to other calendar services such as Google Calendar, iPhone, Android, Microsoft Office 365, etc. This is very useful because it can sync automatically. But what if OutlookCalendar fails to sync with third-party calendars? Possible reasons could be selecting the wrong calendar for synchronization, calendar not visible, background application interference, outdated Outlook application or calendar application, etc. Preliminary fix for Outlook calendar not syncing

Can't open the calendar in the lower right corner of win10 Can't open the calendar in the lower right corner of win10 Dec 26, 2023 pm 05:07 PM

Some friends who use the win0 system have encountered the situation where the win10 calendar cannot be opened. This is just a normal computer glitch. It can be solved in the privacy settings of the win10 system. Today, the editor has brought a detailed solution. Below Let’s take a look. Solution to the problem that the calendar cannot be opened in the lower right corner of win10 1. Click Start in the win10 system → click the program list button above → find Pinyin (Chinese) R → Calendar 2. When using it for the first time, new events may not be opened (mouse If you lean up, there will be no dark blue selected), you can set it in privacy. Click the three-bar icon in the upper left corner of the desktop → there will be a settings menu at the bottom; 3. Click Privacy in the pop-up interface; 4. If you have used settings before, you can click on the left

What should I do if there are no pop-up reminders for calendar events in Win10? How to recover if calendar event reminders are gone in Win10 What should I do if there are no pop-up reminders for calendar events in Win10? How to recover if calendar event reminders are gone in Win10 Jun 09, 2024 pm 02:52 PM

The calendar can help users record your schedule and even set reminders. However, many users are asking what to do if calendar event reminders do not pop up in Windows 10? Users can first check the Windows update status or clear the Windows App Store cache to perform the operation. Let this site carefully introduce to users the analysis of the problem of Win10 calendar event reminder not popping up. To add calendar events, click the "Calendar" program in the system menu. Click the left mouse button on a date in the calendar. Enter the event name and reminder time in the editing window, and click the "Save" button to add the event. Solving the problem of win10 calendar event reminder not popping up

The computer frequently blue screens and the code is different every time The computer frequently blue screens and the code is different every time Jan 06, 2024 pm 10:53 PM

The win10 system is a very excellent high-intelligence system. Its powerful intelligence can bring the best user experience to users. Under normal circumstances, users’ win10 system computers will not have any problems! However, it is inevitable that various faults will occur in excellent computers. Recently, friends have been reporting that their win10 systems have encountered frequent blue screens! Today, the editor will bring you solutions to different codes that cause frequent blue screens in Windows 10 computers. Let’s take a look. Solutions to frequent computer blue screens with different codes each time: causes of various fault codes and solution suggestions 1. Cause of 0×000000116 fault: It should be that the graphics card driver is incompatible. Solution: It is recommended to replace the original manufacturer's driver. 2,

See all articles