php日历制作代码分享


Posted in PHP onJanuary 20, 2014

 
php日历制作代码分享

calendar.class.php

 

 <?php
class Calendar {
  private $year; //当前的年
  private $month; //当前的月
  private $start_weekday; //当月的第一天对应的是周几
  private $days; //当前月一共多少天
  function __construct(){
   $this->year=isset($_GET["year"]) ? $_GET["year"] : date("Y");
   $this->month=isset($_GET["month"]) ? $_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("test.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;
    if($year < 1970)
     $year = 1970;
    $month=12;
   }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++;
    if($year > 2100)
     $year=2100;
    $month=1;
   }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 <= 2100; $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->nextYear($this->year, $this->month).'">'.'>>'.'</a></td>';
   echo '<td><a href="?'.$this->nextMonth($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 相关文章推荐
如何使用PHP获取网络上文件
Oct 09 PHP
通过ODBC连接的SQL SERVER实例
Oct 09 PHP
PHP的开发框架的现状和展望
Mar 16 PHP
php UBB 解析实现代码
Nov 27 PHP
php cookie使用方法学习笔记分享
Nov 07 PHP
一个PHP二维数组排序的函数分享
Jan 17 PHP
php轻松实现中英文混排字符串截取
May 28 PHP
浅析PHP中call user func()函数及如何使用call user func调用自定义函数
Nov 05 PHP
php5.3后静态绑定用法详解
Nov 11 PHP
详解PHP使用Redis存储session时的一个Warning定位
Jul 05 PHP
PHP如何防止XSS攻击与XSS攻击原理的讲解
Mar 22 PHP
Laravel5.1 框架Middleware中间件基本用法实例分析
Jan 04 PHP
php使用qr生成二维码的示例分享
Jan 20 #PHP
php利用新浪接口查询ip获取地理位置示例
Jan 20 #PHP
php利用腾讯ip分享计划获取地理位置示例分享
Jan 20 #PHP
php生成缩略图示例代码分享(使用gd库实现)
Jan 20 #PHP
php解析url的三个示例
Jan 20 #PHP
使用php伪造referer的方法 利用referer防止图片盗链
Jan 20 #PHP
php fsockopen解决办法 php实现多线程
Jan 20 #PHP
You might like
多重?l件?合查?(二)
2006/10/09 PHP
PHP脚本数据库功能详解(上)
2006/10/09 PHP
php 不同编码下的字符串长度区分
2009/09/26 PHP
php checkbox 取值详细说明
2010/08/19 PHP
PHP 函数执行效率的小比较
2010/10/17 PHP
解析PHP中的file_get_contents获取远程页面乱码的问题
2013/06/25 PHP
win7计划任务定时执行PHP脚本设置图解
2014/05/09 PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
2016/03/05 PHP
Smarty环境配置与使用入门教程
2016/05/11 PHP
PHP和MYSQL实现分页导航思路详解
2017/04/11 PHP
tp5框架使用composer实现日志记录功能示例
2019/01/10 PHP
jQuery动画效果-slideUp slideDown上下滑动示例代码
2013/08/28 Javascript
详解JavaScript中的客户端消息框架设计原理
2015/06/24 Javascript
轻松学习jQuery插件EasyUI EasyUI创建RSS Feed阅读器
2015/11/30 Javascript
jQuery内容筛选选择器实例代码
2017/02/06 Javascript
jQuery插件zTree实现更新根节点中第i个节点名称的方法示例
2017/03/08 Javascript
微信小程序搭建自己的Https服务器
2019/05/02 Javascript
JS中的一些常用的函数式编程术语
2019/06/15 Javascript
微信小程序通过websocket实时语音识别的实现代码
2020/08/19 Javascript
解决elementui表格操作列自适应列宽
2020/12/28 Javascript
在Django中使用Sitemap的方法讲解
2015/07/22 Python
解析Python中的__getitem__专有方法
2016/06/27 Python
Django1.7+python 2.78+pycharm配置mysql数据库
2016/10/09 Python
Python FTP两个文件夹间的同步实例代码
2018/05/25 Python
python3解析库BeautifulSoup4的安装配置与基本用法
2018/06/26 Python
Python3.4学习笔记之常用操作符,条件分支和循环用法示例
2019/03/01 Python
python自定义时钟类、定时任务类
2021/02/22 Python
在python里创建一个任务(Task)实例
2020/04/25 Python
白俄罗斯女装和针织品网上商店:Presli.by
2019/10/13 全球购物
使用useBean标志初始化BEAN时如何接受初始化参数
2012/02/11 面试题
庆祝教师节活动方案
2014/01/31 职场文书
2014村务公开实施方案
2014/02/25 职场文书
村党支部公开承诺书
2014/05/29 职场文书
三孔导游词
2015/02/05 职场文书
2016年党员学习廉政准则心得体会
2016/01/20 职场文书
党风廉政建设心得体会(2016最新版)
2016/01/22 职场文书