您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

PHP 自制日历_PHP教程

2024/3/28 15:31:04发布11次查看
php 自制日历 一、计算数据
1、new一个calendar类
2、初始化两个下拉框中的数据,年份与月份
3、初始化要搜索的年份和月份
4、计算得出日历中每一天的数据信息,包括css、天数
复制代码
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;
    }
http://www.bkjia.com/phpjc/885672.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/885672.htmltecharticlephp 自制日历 一、计算数据 1、new一个calendar类 2、初始化两个下拉框中的数据,年份与月份 3、初始化要搜索的年份和月份 4、计算得出日历...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录