用php做个简单的日历
时间:2022-05-07
本文章向大家介绍用php做个简单的日历,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
存档:
index.php
1 <html>
2 <head>
3 <title>日历</title>
4 <style>
5 table{border:1px solid #050;}
6 .fontb{color:white;background:blue;}
7 th{width:30px;}
8 td,th{height:30px;text-align:center;}
9 form{margin:0px;padding:0px;}
10 </style>
11 </head>
12 <body>
13 <?php
14 require "calendar.class.php";
15 echo new Calendar;
16 ?>
17 </body>
18 </html>
calendar.php
1 <?php
2 class Calendar{
3 private $year;
4 private $month;
5 private $start_weekday;
6 private $days;
7 function __construct(){
8 $this->year = isset($_GET["year"])?$_GET["year"]:date("Y");
9 $this->month = isset($_GET["month"])?$_GET["month"]:date("m");
10 $this->start_weekday = date("w",mktime(0,0,0,$this->month,1,$this->year));
11 $this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));
12 }
13
14 function __toString(){
15 $out .='<table align="center">';
16 $out .=$this->chageDate();
17 $out .=$this->weeksList();
18 $out .=$this->daysList();
19 $out .='</table>';
20 return $out;
21 }
22
23 private function weeksList(){
24 $week = array('日','一','二','三','四','五','六');
25 $out .= '<tr>';
26 for($i=0;$i<count($week);$i++){
27 $out .= '<th class="fontb">'.$week[$i].'</th>';
28 }
29 $out .= '</tr>';
30 return $out;
31 }
32
33 private function daysList(){
34 $out .= '<tr>';
35 for($j=0;$j<$this->start_weekday;$j++){
36 $out .= '<td> </td>';
37 }
38 for($k=1;$k<=$this->days;$k++){
39 $j++;
40 if($k==date('d')){
41 $out .= '<td class="fontb">'.$k.'</td>';
42 }
43 else{
44 $out .= '<td>'.$k.'</td>';
45 }
46 if($j%7==0){
47 $out .= '</tr><tr>';
48 }
49 }
50 while($j%7!=0){
51 $out .= '<td> </td>';
52 $j++;
53 }
54 $out .= '</tr>';
55 return $out;
56 }
57
58 private function prevYear($year,$month){
59 $year = $year-1;
60 if($year<1970){
61 $year=1970;
62 }
63 return "year=($year)&month=($month)";
64 }
65
66 private function prevMonth($year,$month){
67 if($month==1){
68 $year=$year-1;
69 if($year<1970){
70 $year=1970;
71 }
72 $month=12;
73 }
74 else{
75 $month--;
76 }
77 return "year=($year)&month=($month)";
78 }
79
80 private function nextYear($year,$month){
81 $year=$year+1;
82 if($year>2038){
83 $year=2038;
84 }
85 return "year=($year)&month=($month)";
86 }
87
88 private function nextMonth($year,$month){
89 if($month==12){
90 $year++;
91 if($year>2038){
92 $year=2038;
93 }
94 $month=1;
95 }
96 else{
97 $month++;
98 }
99 return "year=($year)&month=($month)";
100 }
101
102 private function chageDate($url="index.php"){
103 $out .= '<tr>';
104 $out .= '<td><a href="'.$url.'?'.$this->prevYear($this->year,$this->month).'">'.'<<'.'</a></td>';
105 $out .= '<td><a href="'.$url.'?'.$this->prevMonth($this->year,$this->month).'">'.'<'.'</a></td>';
106 $out .= '<td colspan="3">';
107 $out .= '<form>';
108 $out .= '<select name="year" onchange="window.location=''.$url.'?year='+this.options[selectedIndex].value+'&month='.$this->month.''">';
109 for($sy=1970;$sy<=2038;$sy++){
110 $selected=($sy==$this->year)?"selected":"";
111 $out .='<option '.$selected.' value="'.$sy.'">'.$sy.'</option>';
112 }
113 $out .= '</select>';
114 $out .= '<select name="month" onchange="window.location=''.$url.'?year='.$this->year.'&month='+this.options[selectedIndex].value">';
115 for($sm=1;$sm<=12;$sm++){
116 $selected1=($sm==$this->month)?"selected":"";
117 $out .= '<option '.$selected1.' value="'.$sm.'">'.$sm.'</option>';
118 }
119 $out .= '</select>';
120 $out .= '</form>';
121 $out .= '</td>';
122 $out .= '<td><a href="'.$url.'?'.$this->nextYear($this->year,$this->month).'">'.'>>'.'</a></td>';
123 $out .= '<td><a href="'.$url.'?'.$this->nextMonth($this->year,$this->month).'">'.'>'.'</a></td>';
124 $out .= '</tr>';
125 return $out;
126 }
127 }
128 ?>
结果如下:
- ios 设置label的高度随着内容的变化而变化
- Android字体大小怎么自适应不同分辨率?
- iOS textfield实现一行的数字限制,超出进行弹框
- android https安全连接
- 第二章 正则表达式位置匹配攻略
- 从网络上下载省份城市名称并存入文件然后进行读取省份城市
- android 应用模式之mvp
- Android网络请求框架之Retrofit实践
- iOS 跳转到应用所在的App Store市场
- 第一章 正则表达式字符匹配攻略
- js最新手机号码、电话号码正则表达式
- iOS afnetworking最新版报错 没有AFHTTPRequestOperationManager类了
- Android Service学习之本地服务
- iOS .pch文件的使用
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC