찾다
php教程php手册PHP 自制日历

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;

    }

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구

SecList

SecList

SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.