日历类实现

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. time和datetime模块

    在Python中,通常有这几种方式来表示时间: 1)时间戳 2)格式化的时间字符串  3)元组(struct_time)共九个元素. 由于Python的time模块实现主要调用C库,所以各个平台可能有 ...

  2. python标准库 - socket

    地址簇(address family) socket.AF_UNIX # (UNIX Domain Sockets) socket.AF_INET # ipv4 socket.AF_INET6 # i ...

  3. python3 爬虫笔记(一)beautiful_soup

    很多人学习python,爬虫入门,在python爬虫中,有很多库供开发使用. 用于请求的urllib(python3)和request基本库,xpath,beautiful soup,pyquery这 ...

  4. helm深入学习

    Helm把Kubernetes资源(比如deployments.services或 ingress等) 打包到一个chart中,而chart被保存到chart仓库.通过chart仓库可用来存储和分享c ...

  5. IOS开发入门你们准备好了吗?

    我们对于IOS的了解最多应该就是苹果手机独有的IOS系统吧,也可以说是单任务管理器,这可以说是一个优势,但是随着技术提升IOS慢慢有被超越的趋势,但是很多大公司还是需要这方面的开发人才,那么今天我们来 ...

  6. sublim的正则匹配(待续)

    ctrl+H 打开匹配模式 打开正则匹配模式 正则匹配的一些方法:  点代表的是任意字符.* 代表的是取 0 至 无限长度问号代表的是非贪婪模式.三个链接在一起是取尽量少的任意字符,一般不会这么单独写 ...

  7. 搭建vs2010 boost开发环境

    一.编译boost库 第一步:下载boost库,下载地址http://sourceforge.net/projects/boost/files/boost/1.55.0/ 第二部:解压boost库,例 ...

  8. wxWidgets窗口类型

    如果在创建窗口的时候你没有指定窗口的边框类型,那么在不同的平台上将会有不同的边框类型的缺省值.在windows平台上,控件边框的缺省值为 wxSUNKEN_BORDER,意为使用当前系统风格的边框.你 ...

  9. tensorflow与android编译

    我的过程: 1.下载tensorflow 2.下载ndk.sdk然后放到了tensorflow的目录下 3,修改workspace 4.运行命令:bazel build -c opt //tensor ...

  10. (jdbc和cmd)sqlite数据迁入mysql(导入导出)

    从sqlite进行导出数据 进行cmd命令 第一步:sqlite3->.open [文件路径](打开连接)->.tables(这个是查看表是否有没有)->.cd [切换的盘符](这里 ...