日历类实现

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. MotionEvent的getX(),getY()与getRawX(),getRawY()区别

    在Android的View中getX  getRawX获取的坐标是不一样的,只是相对比照的对象不一样而已. 1.在View中: getX()是表示Widget相对于自身左上角的x坐标,而getRawX ...

  2. LeeCode 第1题

    要求: 给定一个整数(int)数组(Array)和一个目标数值(Target),找出数组中两数之和等于目标值(target)的两个元素的下标位置, 假设:结果唯一,数组中元素不会重复. 本人思路:分别 ...

  3. 01、Scala介绍与安装

    01.Scala介绍与安装 1.1 Scala介绍 Scala是对java语言脚本化,特点是就是使不具备脚本化的java语言能够采用脚本化方式来使用,使其具有脚本语言简单.所见即所得的特点,并且编程效 ...

  4. 从HTTP响应头判断是否命中CDN

    腾讯云: X-Cache-Lookup:Hit From MemCache 表示命中CDN节点的内存X-Cache-Lookup:Hit From Disktank 表示命中CDN节点的磁盘X-Cac ...

  5. Altium_Designer-PCB中各层作用详解

    一直以来,对PCB中各层,比如:solder层.paste层.Top overlay层等等这些一知半解.今天仔细看了下,向大家介绍一下,有不对的地方还请指正. 1.mechanical机械层是定义整个 ...

  6. C# 安装 Visual Studio IDE

    官网: https://visualstudio.microsoft.com/zh-hans/ 下载社区版(免费的) .微软的软件安装都是很nice的.安装过程中选择需要的配置进行安装(比如.net桌 ...

  7. java运行顺序-JVM之九

    简化顺序版本是: 父类的静态成员赋值和静态块,代码排版先后顺序执行,只运行一次 子类的静态成员赋值和静态块,代码排版先后顺序执行,只运行一次 父类代码块 父类构造方法 子类初始化块 子类构造方法 再次 ...

  8. IOS中 copy ,strong ,weak ,assign使用区别

      .@property属性的用法 * weak(assign) :  代理\UI控件 * strong(retain) : 数组.模型)其他对象(除代理\UI控件\字符串以外的对象) * copy ...

  9. Uva 11572 唯一的雪花

    题目链接:https://uva.onlinejudge.org/external/115/11572.pdf 题意:找到一个尽量长的连续子序列 Al ~ AR ,使得该序列没有相同的元素. 分析:枚 ...

  10. 前端jQuery基本语法

    1.概念 1.1基础知识 jQuery是一个兼容多浏览器的JavaScript库,封装了开发过程中常用的一些功能,类似Python模块 jQuery就是用JS写的,JS是基础 jQuery写起来简单, ...