PHP15-日历类实现

日历类实现

1、输出星期

calendar.class.php

 1 <?php
 2     class Calendar{
 3
 4         function out(){//输出表格
 5             echo ‘<table align="center">‘;
 6             $this->weeksList();        //输出星期
 7         8             echo ‘</table>‘;
 9         }
10         private function weeksList(){
11
12             $week=array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘);
13             echo ‘<tr>‘;
14             for($i=0;$i<count($week);$i++)
15                 echo ‘<th class="fontb">‘.$week[$i].‘</th>‘;
16             echo ‘</tr>‘;
17         }
18         19 20 ?>

index.php

 1 <style>            //样式
 2     table {
 3         border:1px solid #050;    //边框,050:绿色
 4     }
 5     .fontb {
 6         color:white;
 7         background:blue;
 8     }
 9
10     th {
11         width=30px;height=30px;
12     }
13 </style>
14 <?php
15     include "calendar.class.php";
16
17     $calendar=new Calendar;
18     $calendar->out();
19 ?

2、输出日期

calendar.class.php

 1 <?php
 2     class Calendar{
 3         private $year;
 4         private $month;
 5         private $start_weekday;    //    当月的第一天是周几
 6         private $days;//当前月有几天
 7
 8         function __construct(){
 9             $this->year=date("Y");
10             $this->month=date("m");
11
12             $this->start_weekday=date("w",mktime(0,0,0,$this->month,1,$this->year));
13             $this->days=date("t",mktime(0,0,0,$this->month,1,$this->year));
14         }
15
16         function out(){//输出表格
17             echo ‘<table align="center">‘;
18             $this->weeksList();
19             $this->daysList();
20             echo ‘</table>‘;
21         }
22         private function weeksList(){
23
24             $week=array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘);
25             echo ‘<tr>‘;
26             for($i=0;$i<count($week);$i++)
27                 echo ‘<th class="fontb">‘.$week[$i].‘</th>‘;
28             echo ‘</tr>‘;
29         }
30         private function daysList(){
31             echo ‘<tr>‘;
32             for($j=0;$j<$this->start_weekday;$j++)
33                 echo ‘<td>&nbsp;</td>‘;        //输出空格
34
35             for($k=1;$k<=$this->days;$k++){
36                 $j++;
37                 if($k==date(‘d‘))        //当前天显示颜色
38                     echo ‘<td class="fontb">‘.$k.‘</td>‘;
39                 else
40                     echo ‘<td>‘.$k.‘</td>‘;
41                 if ($j%7==0)
42                     echo ‘</tr><tr>‘;
43             }
44             echo ‘</tr>‘;
45         }
46     }
47 ?>

test.php

 1 <style>
 2     table {
 3         border:1px solid #050;
 4     }
 5     .fontb {
 6         color:white;
 7         background:blue;
 8     }
 9
10     th {
11         width=30px;
12     }
13     td,th {            //使显示居中
14         height=30px;
15         text-align:center;
16     }
17 </style>
18 <?php
19     include "calendar.class.php";
20
21     $calendar=new Calendar;
22     $calendar->out();
23 ?>

3、

calendar.class.php

  1 <?php
  2     class Calendar{
  3         private $year;
  4         private $month;
  5         private $start_weekday;    //    当月的第一天是周几
  6         private $days;//当前月有几天
  7
  8         function __construct(){
  9             $this->year=isset($_GET["year"])?htmlspecialchars($_GET["year"]):date("Y");
 10             $this->month=isset($_GET["month"])?htmlspecialchars($_GET["month"]):date("m");
 11
 12             $this->start_weekday=date("w",mktime(0,0,0,$this->month,1,$this->year));
 13             $this->days=date("t",mktime(0,0,0,$this->month,1,$this->year));
 14         }
 15
 16         function out(){//输出表格
 17             echo ‘<table align="center">‘;
 18             $this->chageDate();
 19             $this->weeksList();
 20             $this->daysList();
 21             echo ‘</table>‘;
 22         }
 23
 24         private function weeksList(){
 25
 26             $week=array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘);
 27             echo ‘<tr>‘;
 28             for($i=0;$i<count($week);$i++)
 29                 echo ‘<th class="fontb">‘.$week[$i].‘</th>‘;
 30             echo ‘</tr>‘;
 31         }
 32
 33         private function daysList(){
 34             echo ‘<tr>‘;
 35             for($j=0;$j<$this->start_weekday;$j++)
 36                 echo ‘<td>&nbsp;</td>‘;        //输出空格
 37
 38             for($k=1;$k<=$this->days;$k++){
 39                 $j++;
 40                 if($k==date(‘d‘))        //当前天显示颜色
 41                     echo ‘<td class="fontb">‘.$k.‘</td>‘;
 42                 else
 43                     echo ‘<td>‘.$k.‘</td>‘;
 44                 if ($j%7==0)
 45                     echo ‘</tr><tr>‘;
 46             }
 47
 48             //后面几个空格
 49             while($j%7!==0){
 50                 echo ‘<td>&nbsp;</td>‘;
 51                 $j++;
 52             }
 53             echo ‘</tr>‘;
 54         }
 55
 56         private function prevYear($year,$month){
 57             $year=$year-1;
 58             if($year<1970)
 59                 $year=1970;
 60             return "year={$year}&month={$month}";
 61         }
 62         private function prevMonth($year,$month){
 63             if($month==1){
 64                 $year=$year-1;
 65                 $month=12;
 66                 if($year<1970)
 67                     $year=1970;
 68             }else{
 69                 $month--;
 70             }
 71
 72             return "year={$year}&month={$month}";
 73         }
 74         private function nextYear($year,$month){
 75             $year=$year+1;
 76             if($year>2038)
 77                 $year=2038;
 78             return "year={$year}&month={$month}";
 79         }
 80         private function nextMonth($year,$month){
 81             if($month=12){
 82                 $year=$year+1;
 83                 $month=1;
 84                 if($year>2038)
 85                     $year=2038;
 86             }else{
 87                 $month++;
 88             }
 89             return "year={$year}&month={$month}";
 90         }
 91
 92         private function chageDate(){
 93             echo ‘<tr>‘;
 94             echo ‘<td><a href="?‘.$this->prevYear($this->year,$this->month).‘">‘.‘<<‘.‘</a></td>‘;
 95             echo ‘<td><a href="?‘.$this->prevMonth($this->year,$this->month).‘">‘.‘<‘.‘</a></td>‘;
 96             echo ‘<td colspan="3">‘.$this->year.‘年‘.$this->month.‘月‘.‘</td>‘;
 97             echo ‘<td><a href="?‘.$this->nextMonth($this->year,$this->month).‘">‘.‘>‘.‘</a></td>‘;
 98             echo ‘<td><a href="?‘.$this->nextYear($this->year,$this->month).‘">‘.‘>>‘.‘</a></td>‘;
 99             echo ‘</tr>‘;
100         }
101     }
102 ?>

4、增加年月选择下拉列表  *

calendar.calss.php

<?php
    class Calendar{
        private $year;
        private $month;
        private $start_weekday;    //    当月的第一天是周几
        private $days;//当前月有几天

        function __construct(){
            $this->year=isset($_GET["year"])?htmlspecialchars($_GET["year"]):date("Y");
            $this->month=isset($_GET["month"])?htmlspecialchars($_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("text.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;
                $month=12;
                if($year<1970)
                    $year=1970;
            }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=$year+1;
                $month=1;
                if($year>2038)
                    $year=2038;
            }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->nextMonth($this->year,$this->month).‘">‘.‘>‘.‘</a></td>‘;
            echo ‘<td><a href="?‘.$this->nextYear($this->year,$this->month).‘">‘.‘>>‘.‘</a></td>‘;
            echo ‘</tr>‘;
        }
    }
?>

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();
?>

时间: 2024-10-12 23:30:21

PHP15-日历类实现的相关文章

iOS 日历类(NSCalendar)

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

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

日历类

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: //

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.clas

Java中的日历类/集合类/数学类/正则表达式/数组工具类等的常用方法与基本功能

一. Arrays 针对数组操作的工具类,提供了一些针对数组排序和二分搜索的方法.常用方法:1.public static String toString(int[] a):将int类型的数组转换成字符串.转换成字符串后输出形式: ([元素1,元素2,元素3...]),Arrays在底层重写了toString方法.2.public static void sort(int[] a):对指定的 int 型数组按数字升序进行排序.3.public static int binarySearch(in

Date日期类,Canlendar日历类,Math类,Random随机数学类

Date日期类,SimpleDateFormat日期格式类 Date  表示特定的时间,精确到毫秒 常用方法 getTime() setTime() before() after() compareTo() 比较 toString() DateFormat是日期/时间格式化抽象类 SimpleDateFormat日期/时间格式化子类 SimpleDateFormat(模板字符串) 常用方法: format()  Date转换成字符串 parse ()  字符串转换成Date package co

java_日历类

calendar是日历类,该类是抽象类不能被实例化 1 public class CalendarTest { 2 /* 3 创建对象和方法的使用 4 */ 5 public static void main(String[] args) { 6 //实例化对象 7 Calendar instance = Calendar.getInstance(); 8 //说明是子类实现class java.util.GregorianCalendar 9 System.out.println(instan

Calendar是日历类

Calendar是日历类,在Date后出现,替换掉了许多Date的方法.该类将所有可能用到的时间信息封装为静态成员变量,方便获取. Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,将语言敏感内容处理好,再返回子类对象 Calendar类常用方法 l  public static Calendar getInstance() //获取日期对象 l  public int get(int field)   //获取时间字段值,字段参见帮助文