日历类实现

1、输出星期

calendar.class.php

 <?php
class Calendar{ function out(){//输出表格
echo '<table align="center">';
$this->weeksList();        //输出星期
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>';
}
?>

index.php

 <style>            //样式
table {
border:1px solid #050; //边框,050:绿色
}
.fontb {
color:white;
background:blue;
} th {
width=30px;height=30px;
}
</style>
<?php
include "calendar.class.php"; $calendar=new Calendar;
$calendar->out();
?

2、输出日期

calendar.class.php

 <?php
class Calendar{
private $year;
private $month;
private $start_weekday; // 当月的第一天是周几
private $days;//当前月有几天 function __construct(){
$this->year=date("Y");
$this->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->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>';
}
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;
}
</style>
<?php
include "calendar.class.php"; $calendar=new Calendar;
$calendar->out();
?>

3、

calendar.class.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();
$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(){
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">'.$this->year.'年'.$this->month.'月'.'</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>';
}
}
?>

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

PHP.13-日历类实现的更多相关文章

  1. NSCalendar 日历类

    NSCalendar 日历类 Cocoa中对日期和时间的处理 NSCalendar (一) (2008-11-12 21:54:10) NSCalendar用于处理时间相关问题.比如比较时间前后.计算 ...

  2. java基础22 日期类、日历类、日期格式类

    package com.dhb.code; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  3. 31.Java基础_日期/日期格式/日历类

    Date类 Date对象构造方法 Date对象常用方法 import java.util.*; public class test { public static void main(String[] ...

  4. PHP中的国际化日历类

    在 PHP 的国际化组件中,还有一个我们并不是很常用的跟日期相关的操作类,它就是日历操作类.说是日历,其实大部分还是对日期时间的操作,一般也是主要用于日期的格式化和比较之类的.但是通常我们直接使用 d ...

  5. NSCalenda日历类

    1. //将数据库时间和当前时间相比,得出时间差. + (NSString *)dateDescriptionWithDate:(NSDate *)date{ // NSCalendar日历类,提供了 ...

  6. iOS 日历类(NSCalendar)

    对于时间的操作在开发中很常见,但有时候我们需要获取到一年后的时间,或者一周后的时间.靠通过秒数计算是不行的.那就牵扯到另外一个日历类(NSCalendar).下面先简单看一下 NSDate let d ...

  7. java学习笔记之日期日历类

    java学习笔记之日期日历 Date日期类概述: 表示特定的瞬间,精确到毫秒 Date类的构造方法: 1.空参数构造方法 Date date = new Date(); 获取到当前操作系统中的时间和日 ...

  8. 日历类和日期类转换 并发修改异常 泛型的好处 *各种排序 成员和局部变量 接口和抽象类 多态 new对象内存中的变化

    day07 ==和equals的区别? ==用于比较两个数值 或者地址值是否相同.  equals 用于比较两个对象的内容是否相同   String,StringBuffer.StringBuilde ...

  9. PHP设计日历类一 (38)

    由两个文件组成: 第一个test.php <style> table { border:1px solid #; } .fontb { color:white; background:bl ...

  10. 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类

    Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...

随机推荐

  1. Eclipse常用设置和快捷键

    1.提示键配置一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项 ...

  2. Windows下hosts文件的作用

    原文地址:https://my.oschina.net/u/874225/blog/194348 在操作系统中的路径:Win7在C:\Windows\System32\drivers\etc目录下 内 ...

  3. nodejs+MQTT协议实现远程主机控制

    摘抄自百度:MQTT(MessageQueuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分. 所谓物联网,就是“万物互 ...

  4. 2016 Multi-University Training Contest 2 - 1005 (hdu5738)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...

  5. 寄生组合式继承 js

    寄生组合式继承是集寄生式继承和组合继承的优点于一身,是基于类型继承最有效的方式 function object(o){ function F(){}; F.prototype = o; return ...

  6. Edmonds-Karp算法,最大流POJ(1459)

    题目链接:http://poj.org/problem?id=1459 解题报告: 电力调度站不涉及流的产生和消耗,不用考虑,Edmonds-Karp算法,就是利用剩余网络和增广路来解决,网络中的最大 ...

  7. 问题 B: 矩形类中运算符重载【C++】

    题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数.输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数.要求使用提示中给出的测试函数并不得改动. 两个矩 ...

  8. c#无限循环

    for( ; ; ) 最快的 while(true) while(1)             ?好像也是不过就是扫到的 public bool a= true; 中断一个循环while(a) a=f ...

  9. Git基础篇

    对于Git的一些基础了解,安装,里面的一些名词,这里就不做介绍了.主要记录怎么使用GIt. 本篇介绍: 配置个人信息        生成本地仓库并与远程库相连        添加SSH秘钥       ...

  10. JavaScript正则(一)

    1.字符组: ^  $ 说的是开始位置和结束位置,在JS中,既表示字符串的起始位置和结束位置,也表示行的起始位置和结束位置 console.log(/^\d$/.test('2')); // true ...