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 相关文章推荐
一键删除顽固的空文件夹 软件下载
Jan 26 PHP
随时给自己贴的图片加文字的php水印
Mar 16 PHP
PHP array_multisort() 函数的深入解析
Jun 20 PHP
php图像处理函数大全(推荐收藏)
Jul 11 PHP
php+mysqli使用面向对象方式更新数据库实例
Jan 29 PHP
汇总PHPmailer群发Gmail的常见问题
Feb 24 PHP
PHP入门教程之上传文件实例详解
Sep 11 PHP
Zend Framework入门应用实例详解
Dec 11 PHP
ThinkPHP中调用PHPExcel的实现代码
Apr 08 PHP
PHP面向对象五大原则之单一职责原则(SRP)详解
Apr 04 PHP
PHP笛卡尔积实现算法示例
Jul 30 PHP
PHP chop()函数讲解
Feb 11 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
关于我转生变成史莱姆这档事:第二季PV上线,萌王2021年回归
2020/05/06 日漫
JS与PHP向函数传递可变参数的区别实例代码
2011/05/18 PHP
解析php如何将日志写进syslog
2013/06/28 PHP
php中socket的用法详解
2014/10/24 PHP
基于PHP实现通过照片获取ip地址
2016/04/26 PHP
下载站控制介绍字数显示的脚本 显示全部 隐藏介绍等功能
2009/09/19 Javascript
jQuery Ajax之load()方法
2009/10/12 Javascript
JQuery最佳实践之精妙的自定义事件
2010/08/11 Javascript
js+css实现有立体感的按钮式文字竖排菜单效果
2015/09/01 Javascript
js根据手机客户端浏览器类型,判断跳转官网/手机网站多个实例代码
2016/04/30 Javascript
JS图片等比例缩放方法完整示例
2016/08/03 Javascript
Mvc提交表单的四种方法全程详解
2016/08/10 Javascript
简单三步实现报表页面集成天气
2016/12/15 Javascript
Angular2学习教程之组件中的DOM操作详解
2017/05/28 Javascript
Node.js 实现简单的无侵入式缓存框架的方法
2019/07/21 Javascript
Vue路由管理器Vue-router的使用方法详解
2020/02/05 Javascript
python使用datetime模块计算各种时间间隔的方法
2015/03/24 Python
python实现一个简单的并查集的示例代码
2018/03/19 Python
详谈python在windows中的文件路径问题
2018/04/28 Python
利用anaconda保证64位和32位的python共存
2021/03/09 Python
python 字典有序并写入json文件过程解析
2019/09/30 Python
基于Python实现扑克牌面试题
2019/12/11 Python
详解opencv中画圆circle函数和椭圆ellipse函数
2019/12/27 Python
Python读取YAML文件过程详解
2019/12/30 Python
python从内存地址上加载python对象过程详解
2020/01/08 Python
Python内存映射文件读写方式
2020/04/24 Python
Python 按比例获取样本数据或执行任务的实现代码
2020/12/03 Python
收集的7个CSS3代码生成工具
2010/04/17 HTML / CSS
荷兰优雅女装网上商店:Heine
2016/11/14 全球购物
生物科学系大学生的自我评价
2013/12/20 职场文书
党员自我批评与反省材料
2014/02/10 职场文书
安全生产年活动总结
2014/08/29 职场文书
2015年环保局工作总结
2015/05/22 职场文书
水知道答案观后感
2015/06/08 职场文书
新手开公司创业注意事项有哪些?
2019/07/29 职场文书
python scipy 稀疏矩阵的使用说明
2021/05/26 Python