Table of Contents
PHP 自制日历
Home php教程 php手册 PHP 自制日历

PHP 自制日历

Jun 13, 2016 am 09:24 AM
calendar

PHP 自制日历

一、计算数据

 

 

 

1、new一个Calendar类

 

2、初始化两个下拉框中的数据,年份与月份

 

3、初始化要搜索的年份和月份

 

4、计算得出日历中每一天的数据信息,包括css、天数

 

复制代码

    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

?>

复制代码

 

 

二、html展示

 

 

 

1、休息天的背景色是不同的,不是当前搜索年月的天数字体颜色也是不同的

 

2、div中做初始化年份与月份的下拉框的操作,并选中当前要搜索的年月

 

3、数据已计算好,哪个td属于哪个tr也已做好,直接将table打印出来即可

 

复制代码

  

       

       

           

       

       

       

       

           

       

       

       

   

   

       

               

                   

                   

                   

                   

                   

                   

                   

               

       

       

           

               

               

                   

                   

               

           

       

   

                       

                   

复制代码

 

 

三、Calendar类

 

 

 

1、threshold方法,生成日历的各个边界值

 

  1)计算这个月总天数

 

  2)计算这个月第一天与最后一天,各是星期几

 

  3)计算日历中的第一个日期与最后一个日期

 

复制代码

  /**

     * @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方法,计算日历的天数与样式

 

  1)将上个月的天数计算出来,本月第一天的星期不是星期天的话,就需要根据上个月的最后一天计算

 

  2)将本月的天数遍历出来,如果是休息天就加上特殊的css样式

 

  3)将下个月的天数计算出来,分三种情况,星期日、星期六和工作日

 

复制代码

  /**

     * @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

                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

            $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

            array_push($dates, array('day' => $i, 'tdclass' => ($i==$length ?'rest':''), 'pclass' => 'outter'));

        }

        

        return $dates;

    }

复制代码

 

 

3、draw方法,画表格,设置table中的tr与td

 

  1)数据将要用table标签来显示,所以这里要将各个tr下面的td排列好

 

  2)$index % 7 == 0 计算表格每行的第一列

 

  3)$index % 7 == 6 || $index == ($length-1) 计算每行的最后一列,或$caculate的最后一个数据

 

  4)将中间行添加到$tr中,就是每一行的array

 

复制代码

  /**

     * @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;

    }

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)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
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

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.

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

How to solve the problem that the time in win11 is always inaccurate? Win11 time adjustment tutorial quickly solves the problem of inaccurate time How to solve the problem that the time in win11 is always inaccurate? Win11 time adjustment tutorial quickly solves the problem of inaccurate time Apr 19, 2024 am 09:31 AM

If your Windows 11 computer displays the wrong time, it can cause a lot of problems and even prevent you from connecting to the internet. In fact, some applications refuse to open or run when the system displays an incorrect date and time. So how should this problem be solved? Let’s take a look below! Method 1: 1. We first right-click on the blank space of the taskbar below and select Taskbar Settings 2. Find taskbarcorneroverflow3 on the right in the taskbar settings, then find clock or clock above it and select to turn it on. Method 2: 1. Press the keyboard shortcut win+r to call up run, enter regedit and press Enter to confirm. 2. Open the Registry Editor and find HKEY in it

How to stop Outlook from automatically adding events to my calendar How to stop Outlook from automatically adding events to my calendar Feb 26, 2024 am 09:49 AM

As an email manager application, Microsoft Outlook allows us to schedule events and appointments. It enables us to stay organized by providing tools to create, manage and track these activities (also called events) in the Outlook application. However, sometimes unwanted events are added to the calendar in Outlook, which creates confusion for users and spams the calendar. In this article, we will explore various scenarios and steps that can help us prevent Outlook from automatically adding events to my calendar. Outlook Events – A brief overview Outlook events serve multiple purposes and have many useful features as follows: Calendar Integration: In Outlook

No Period Lost Purchasing Office: New calendar and birthday series peripherals! No Period Lost Purchasing Office: New calendar and birthday series peripherals! Feb 29, 2024 pm 12:00 PM

The Lost Purchasing Office is confirmed to be updated at 11 am on February 28th. Players can go to Taobao to search the Purchasing Office and select the store category to purchase. This time we bring you the MBCC birthday party series and 2024 Desk Calendar peripherals. Come together. Take a look at the product details this time. No Period Lost Purchasing Office: New calendar and birthday series peripherals! There is something new in the Lost Procurement Office! - Pre-sale time: February 28, 2024 11:00 - March 13, 2024 23:59 Purchase address: Taobao search [Unexpected Lost Purchasing Office] Select [Store] category to enter the store for purchase; peripheral introduction: The new peripherals released this time are MBCC birthday party series and 2024 desk calendar peripherals. Please click on the long image for details. The Purchasing Office introduces new peripherals—MBCC students

See all articles