日历类实现

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. C#变量、常量

    变量 一.命名变量:标识符 为变量命名时要遵循C#语言的命名规范: 1.变量名只能由字母.数字和下划线组成,而不能包含空格.标点符号.运算符等其他符号 2.变量名不能与C#中的关键字名称相同   二. ...

  2. lucene查询解析器语法

    注意:使用QueryParser查询,关键词是会被分词的,如果不需要分词,可以选择使用Lucene提供的API查询类. Lucene提供了丰富的API来组合定制你所需要的查询器,同时也可以利用Quer ...

  3. python3线程介绍01(如何启动和调用线程)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...

  4. Python基础学习之字符串(2)

    字符串常用方法 1.s.capitalize() 描述:返回字符串s的副本,并将首字符变为大写. 示例: >>> s='yesterday when I was Young!' &g ...

  5. Selenium入门12 鼠标和键盘事件

    1 鼠标 集成在webdriver.ActionChains.单击.双击.右击.拖放等等.   2 键盘 引入包from selenium.webdriver.common.keys import K ...

  6. ie6下按钮下边框消失不显示的问题

    最近网站做改版,又发现一个ie6奇葩的问题,就一个很普通带边框的按钮,但在ie6中下边框不显示,ie7没有测试不知道是不是也不显示,其他浏览器正常 代码和预览效果如下: <style> b ...

  7. 数黑格有多少个,模拟题,POJ(1656)

    题目链接:http://poj.org/problem?id=1656 #include <stdio.h> #include <iostream> #include < ...

  8. Android Support v4,v7,v13的区别和应用场景

    android-support-v4 是谷歌推出的兼容包,最低兼容Android1.6的系统,里面有类似ViewPager等控件.ViewPager在Android 1.6以下的版本是不自带的,所以要 ...

  9. AngularJS 表达式中添加过滤器实例

    过滤器可以通过一个管道字符(|)和一个过滤器添加到表达式中 历练实例: <!DOCTYPE html><html><head><meta http-equiv ...

  10. 关于package.json学习

    1.如果要下载npm包,必须有package.json文件,不然会报错,如果缺少必要字符报错,参考报错信息 2.license,指定用户权限,可以不写,不会报错 3.devDependencies,依 ...