PHP设计日历类一 (38)

由两个文件组成:

第一个test.php

<style>
    table {
        border:1px solid #050;
    }

    .fontb {
        color:white;
        background:blue;
    }

    th {
        width:30px;
    }

    td,th {
        height:30px;
        text-align:center;

    }
    form {
        margin:0px;
        padding:0px;
    }
</style>
<?php
    include "calendar.class.php";

    $calendar=new Calendar;

    $calendar->out();

第二个日历类:calendar.class.php

<?php
class Calendar {
        private $year; //当前的年
        private $month; //当前的月
        private $start_weekday; //当月的第一天对应的是周几
        private $days; //当前月一共多少天

        function __construct(){
            $this->year=isset($_GET["year"]) ? $_GET["year"] : date("Y");
            $this->month=isset($_GET["month"]) ? $_GET["month"] : date("m");

            $this->start_weekday=date("w", mktime(0, 0, 0, $this->month, 1, $this->year));
            $this->days=date("t", mktime(0, 0, 0, $this->month, 1, $this->year));
        }

        function out(){
            echo ‘<table align="center">‘;
            $this->chageDate("test.php");
            $this->weeksList();
            $this->daysList();
            echo ‘</table>‘;
        }

        private function weeksList(){
            $week=array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘);

            echo ‘<tr>‘;
            for($i=0; $i<count($week); $i++)
                echo ‘<th class="fontb">‘.$week[$i].‘</th>‘;

            echo ‘</tr>‘;
        }

        private function daysList(){
            echo ‘<tr>‘;
            //输出空格(当前一月第一天前面要空出来)
            for($j=0; $j<$this->start_weekday; $j++)
                echo ‘<td>&nbsp;</td>‘;

            for($k=1; $k<=$this->days; $k++){
                $j++;
                if($k==date(‘d‘))
                    echo ‘<td class="fontb">‘.$k.‘</td>‘;
                else
                    echo ‘<td>‘.$k.‘</td>‘;

                if($j%7==0)
                    echo ‘</tr><tr>‘;

            }

            //后面几个空格
            while($j%7!==0){
                echo ‘<td>&nbsp;</td>‘;
                $j++;
            }

            echo ‘</tr>‘;
        }

        private function prevYear($year, $month){
            $year=$year-1;

            if($year < 1970)
                $year = 1970;

            return "year={$year}&month={$month}";
        }

        private function prevMonth($year, $month){
            if($month == 1) {
                $year = $year -1;

                if($year < 1970)
                    $year = 1970;

                $month=12;
            }else{
                $month--;
            }

            return "year={$year}&month={$month}";
        }

        private function nextYear($year, $month){
            $year = $year + 1;

            if($year > 2038)
                $year = 2038;

            return "year={$year}&month={$month}";
        }

        private function nextMonth($year, $month){
            if($month==12){
                $year++;

                if($year > 2038)
                    $year=2038;

                $month=1;
            }else{
                $month++;
            }

            return "year={$year}&month={$month}";
        }

        private function chageDate($url=""){
            echo ‘<tr>‘;
            echo ‘<td><a href="?‘.$this->prevYear($this->year, $this->month).‘">‘.‘<<‘.‘</a></td>‘;
            echo ‘<td><a href="?‘.$this->prevMonth($this->year, $this->month).‘">‘.‘<‘.‘</a></td>‘;
            echo ‘<td colspan="3">‘;
            echo ‘<form>‘;
            echo ‘<select name="year" onchange="window.location=\‘‘.$url.‘?year=\‘+this.options[selectedIndex].value+\‘&month=‘.$this->month.‘\‘">‘;
            for($sy=1970; $sy <= 2038; $sy++){
                $selected = ($sy==$this->year) ? "selected" : "";
                echo ‘<option ‘.$selected.‘ value="‘.$sy.‘">‘.$sy.‘</option>‘;
            }
            echo ‘</select>‘;
            echo ‘<select name="month"  onchange="window.location=\‘‘.$url.‘?year=‘.$this->year.‘&month=\‘+this.options[selectedIndex].value">‘;
            for($sm=1; $sm<=12; $sm++){
                $selected1 = ($sm==$this->month) ? "selected" : "";
                echo ‘<option ‘.$selected1.‘ value="‘.$sm.‘">‘.$sm.‘</option>‘;
            }
            echo ‘</select>‘;
            echo ‘</form>‘;
            echo ‘</td>‘;

            echo ‘<td><a href="?‘.$this->nextYear($this->year, $this->month).‘">‘.‘>>‘.‘</a></td>‘;
            echo ‘<td><a href="?‘.$this->nextMonth($this->year, $this->month).‘">‘.‘>‘.‘</a></td>‘;
            echo ‘</tr>‘;
        }

    }

效果:

时间: 2024-08-03 12:21:38

PHP设计日历类一 (38)的相关文章

日历类

1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: using System.Text.RegularExpressions; 6: using System.Globalization; 7:   8: namespace AnkeEdu.Tools 9: { 10: /// <summary> 11: /// 中国日历信息实体类 12: //

设计计算机类要求如下:属性:品牌、颜色、cpu型号,内存容量,硬盘大小,价格,工作状态;方法:打开,关闭,休眠;创建一个计算机对象,调用打开、关闭方法

代码如下: 1 //作者:realjanushu 2 //日期:17/9/28 3 /*功能: 4 设计计算机类要求如下: 5 6 属性:品牌.颜色.cpu型号,内存容量,硬盘大小,价格,工作状态: 7 8 方法:打开,关闭,休眠: 9 10 创建一个计算机对象,调用打开.关闭方法 11 */ 12 public class ComputerDemo{ 13 public static void main(String[] args){ 14 Computer c1 = new Computer

从设计基类及其派生类看继承关系

继承能够定义可重用.扩展或修改父类行为的子类.但基类的静态构造函数.实例构造函数和析构函数不能被派生类继承. 在下面实例中,定义一个基类Publication用于表示任何类型的出版物以及派生至Publication的其他类型Book类,由此也可以扩展为定义其他类型如:Magazine.Journal.Newspaper和Article. 在设计基类Publication时我们必须考虑到如下关系: 1.要在基类中添加哪些成员 2.基类是否用作派生类模板的抽象基类 3.类层次结构的扩展空间大小,要开

iOS 日历类(NSCalendar)

对于时间的操作在开发中很常见,但有时候我们需要获取到一年后的时间,或者一周后的时间.靠通过秒数计算是不行的.那就牵扯到另外一个日历类(NSCalendar).下面先简单看一下 NSDate let date = NSDate()let formatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd HH-mm-ss"formatter.stringFromDate(date)// 延迟多少秒为正数 前多少秒为负数let

作业题---设计一个类,求和的方法,求乘积的方法

设计一个类: 包含$a,$b,求和的方法,求乘积的方法,  可以对变量进行初始化, $a,$b必须大于0小于100 class suanshu{ private $shuzi1; private $shuzi2; function __construct($a,$b) { $this->shuzi1 = $a; $this->shuzi2 = $b; } function __set($n,$v) { if($v>0 and $v<100) { $this->$n = $v;

设计CTime类,并且在CTime类中使用运算符重载

程序代码: #include <iostream> using namespace std; class CTime//时间类 { private: unsigned short int hour; //时 unsigned short int minute; //分 unsigned short int second; //秒 public: CTime(int h=0,int m=0,int s=0);//构造函数 void setTime(int h,int m,int s);//设置时

Java Calendar日历类的使用

Calendar cal = Calendar.getInstance(); // 当前年 int year = cal.get(Calendar.YEAR); // 当前月 int month = (cal.get(Calendar.MONTH)) + 1; // 当前月的第几天:即当前日 int day_of_month = cal.get(Calendar.DAY_OF_MONTH); // 当前时:HOUR_OF_DAY(24小时制):HOUR(12小时制) int hour = cal

java日历类Calendar简单使用

import java.util.Calendar; import java.util.TimeZone; public class Test1 { public static void main(String[] args) { //指定东八区,即北京时间 Calendar cc = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); int year = cc.get(Calendar.YEAR); //月份是从0开始计数的,所

自写打印日历类

在论坛上看到有人在问打印日历的程序,今天下午空闲的时候写了一个日历类,简单的写了几个方法. calendar.h #include <string> using namespace std; #ifndef CALENDAR_H #define CALENDAR_H class Calendar{ public: Calendar(){} void printAllMonth(const int &year); void printOneMonth(const int &yea