PHP.13-日历类实现
日历类实现
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> </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> </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> </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> </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> </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-日历类实现的更多相关文章
- NSCalendar 日历类
NSCalendar 日历类 Cocoa中对日期和时间的处理 NSCalendar (一) (2008-11-12 21:54:10) NSCalendar用于处理时间相关问题.比如比较时间前后.计算 ...
- java基础22 日期类、日历类、日期格式类
package com.dhb.code; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...
- 31.Java基础_日期/日期格式/日历类
Date类 Date对象构造方法 Date对象常用方法 import java.util.*; public class test { public static void main(String[] ...
- PHP中的国际化日历类
在 PHP 的国际化组件中,还有一个我们并不是很常用的跟日期相关的操作类,它就是日历操作类.说是日历,其实大部分还是对日期时间的操作,一般也是主要用于日期的格式化和比较之类的.但是通常我们直接使用 d ...
- NSCalenda日历类
1. //将数据库时间和当前时间相比,得出时间差. + (NSString *)dateDescriptionWithDate:(NSDate *)date{ // NSCalendar日历类,提供了 ...
- iOS 日历类(NSCalendar)
对于时间的操作在开发中很常见,但有时候我们需要获取到一年后的时间,或者一周后的时间.靠通过秒数计算是不行的.那就牵扯到另外一个日历类(NSCalendar).下面先简单看一下 NSDate let d ...
- java学习笔记之日期日历类
java学习笔记之日期日历 Date日期类概述: 表示特定的瞬间,精确到毫秒 Date类的构造方法: 1.空参数构造方法 Date date = new Date(); 获取到当前操作系统中的时间和日 ...
- 日历类和日期类转换 并发修改异常 泛型的好处 *各种排序 成员和局部变量 接口和抽象类 多态 new对象内存中的变化
day07 ==和equals的区别? ==用于比较两个数值 或者地址值是否相同. equals 用于比较两个对象的内容是否相同 String,StringBuffer.StringBuilde ...
- PHP设计日历类一 (38)
由两个文件组成: 第一个test.php <style> table { border:1px solid #; } .fontb { color:white; background:bl ...
- 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类
Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...
随机推荐
- 创建Podspec 并且发布到github spec
昨天,花了点时间,把自己的代码做成framework,但是发现,每次迁移项目或者更新项目都是一件很头疼的事情,索性,也跟着时尚了一回,把所有代码都扔到git里面进行管理,通过cococapods直接安 ...
- 用SQL将数字转换为中文数字
IF OBJECT_ID('fn_GetChnNum') IS NOT NULL BEGIN DROP FUNCTION dbo.fn_GetChnNum; END; GO CREATE FUNCTI ...
- python3 爬虫笔记(一)beautiful_soup
很多人学习python,爬虫入门,在python爬虫中,有很多库供开发使用. 用于请求的urllib(python3)和request基本库,xpath,beautiful soup,pyquery这 ...
- Visual Studio 编辑器打开项目后,一直提醒Vs在忙,解决方法
今天打开VS2015后,因为这个解决中有很项目,突然就一直现在加载中,点击VS提示在忙,怎么破那?请往下看 第一种方法 1.关闭VS: 2.去C:\Users\<your users name& ...
- do..while(false)的用法总结
首先要注意: do..while(0) 代表do里面的东西至少被执行一次,在这里仅仅执行一次. 此种用法有三个用处: 代替{}代码块,实现局部作用域.在某些宏定义时非常有用: #define f(x) ...
- Git由来
很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与 ...
- selenium项目--读取测试用例
读取测试用例 一直我们都没有考虑过读取测试用例的事,我们现在这样设计测试用例有两个好的点,在执行方法时,打印测试用例,方便知道执行的内容是什么,在报告展示时,把测试用例的结果展示出来 实现方案:目前我 ...
- 高精度进位制转换,Poj(1220)
转自ACdream. #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXS ...
- Performing User-Managed Database-18.5、Restoring Control Files
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/offbeatmine/article/details/28429339 18.5.Restoring ...
- JS判断手机横竖屏
在移动端开发时,有时候需要判断手机的横竖屏,那么就需要用到window.orientation这个参数,通过这个参数可以判断出手机是处在横屏还是竖屏状态. 屏幕方向对应的window.orientat ...