PHP实现简单日历类编写

 更新时间:2020年08月28日 11:43:29   作者:shofe的菜鸟人生  
这篇文章主要为大家详细介绍了PHP实现简单日历类编写,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用PHP实现日历类的编写,供大家参考,具体内容如下

calendar.class.php

<?php
/*
* 创建一个日历类
*
*
*/
 //修改默认时区
 date_default_timezone_set("PRC");
 
 class Calendar {
  private $year;
 private $month;
 private $day; //当月总天数
 private $first_week; //每月的第一天是星期几
 
 //构造函数
 function __construct() {
  $this->year = isset($_GET['year'])?$_GET['year']:date("Y");
  $this->month = isset($_GET["month"])?$_GET["month"]:date("m");
  $this->first_week = date("w", mktime(0, 0 ,0, $this->month, 1, $this->year));
  $this->day = date("t", mktime(0, 0 ,0, $this->month, 1, $this->year));
 }
 function showCalendar() {
 //  echo $this->year."年".$this->month."月".$this->first_week."天".$this->day;
   echo "<table align='center'>"; //用表格输出
   $this->chageDate("index.php"); //用于用户调整年月份
  $this->weekList();//显示星期
  $this->dayList(); //显示天数
  
  echo "</table>";
 }
 //1、显示星期
 private function weekList() {
  $week = array("日","一","二","三","四","五","六");
  echo "<tr>";
   for ($i = 0; $i < count($week); $i++) {
   echo "<th>".$week[$i]."</th>";
  }
  echo "</tr>";
 }
 //2.显示天数
 private function dayList() {
  $color = "#2ca50c";
  echo "<tr>";
  for ($i = 0; $i < $this->first_week; $i++) { //输出空格,弥补当前月空缺部分
   echo "<td bgcolor='#2ca50c'> </td>";
  }
  for ($k = 1; $i <= $this->day; $k++) {
   $i++;
   if ($k == date("d")) echo "<td id='nowd'>".$k."</td>"; //是今天,加效果
   else echo "<td bgcolor=$color>".$k."</td>";
   if ($i % 7 == 0) {
   echo "</tr><tr>"; //每7天一次换行
   if ($i % 2 == 0) $color = "#2ca50c";
   else $color = "#9ddb27"; //实现各行换色的效果
   }
  }
  while ($i % 7 != 0) { //将剩余的空格补完
   echo "<td bgcolor='#2ca50c'> </td>";
  $i++; 
  }
  echo "</tr>";
 }
  
 //3、用于用户调整天数
 private function chageDate($url="index.php") {
  echo "<tr>";
   echo "<caption><h1>".$this->year."年".$this->month."月</h1></caption>"; 
  echo "</tr>";
  echo "<tr>";
  echo "<td>"."<a href='?".$this->prevYear($this->year,$this->month)."'>"."<"."</a>";
  echo "<td>"."<a href='?".$this->prevMonth($this->year,$this->month)."'>"."<<"."</a>";
  
  echo "<td colspan='3'>";
   echo '<select οnchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
    for ($year = 2038; $year >= 1970; $year--) {
    $selected = ($year == $this->year)?"selected":"";
    echo '<option '.$selected. ' value="'.$year.'">'.$year.'</option>';
    //echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
   }
   echo "</select>";
   
  echo '<select name="month" οnchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
  for($month=1;$month <= 12;$month++){
   $selected1 = ($month == $this->month) ? "selected" : "";
   echo '<option '.$selected1.' value="'.$month.'">'.$month.'</option>';
  }
  echo '</select>';
  echo "</td>";
  
  
  echo "<td>"."<a href='?".$this->nextMonth($this->year,$this->month)."'>".">>"."</a>";
  echo "<td>"."<a href='?".$this->nextYear($this->year,$this->month)."'>".">"."</a>";
  echo "</tr>";
 }
 
 private function prevYear($year, $month) { //获取上一年的数据
  $year--;
  if ($year < 1970) $year = 1970;
  return "year={$year}&month={$month}";
 }
 private function prevMonth($year, $month) {
  if ($month == 1) {
   $year--;
  if ($year < 1970) $year = 1970;
  $month = 12;
  }else $month--; 
  return "year={$year}&month={$month}";
 }
 private function nextYear($year, $month) { //获取上一年的数据
  $year++;
  if ($year > 2038) $year = 2038;
  return "year={$year}&month={$month}";
 }
 private function nextMonth($year, $month) {
  if ($month == 12) {
   $year++;
  if ($year > 2038) $year = 2038;
  $month = 1;
  }else $month++; 
  return "year={$year}&month={$month}";
 }
 }

主页 index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>日历显示</title>
<style>
 table {
 border:1px solid #050;
 margin: 100px auto;
 }
 th {
  width: 30px;
 background-color: #0CC;
 color: #fff;
 height: 30px;
 font-size: 20px;
 }
 #nowd {
  color: yellow;
 background: #F00;
 }
 td {
  width: 30px;
 text-align: center;
 
 height: 25px;
 color: #fff;
 }
 a {
 display: block;
 width: 35px;
 height: 35px;
 background: #0F9;
  text-decoration: none;
 text-align: center;
 line-height: 35px;
 }
 a:hover {
  background: #CF0;
 color: #fff;
 font-size: 20px;
 }
</style>
</head>
 
<body>
 <?php
 include "calendar.class.php";
 $ca = new Calendar();
 $ca->showCalendar();
 ?>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • php数据库抽象层 PDO

    php数据库抽象层 PDO

    因为这段时间工作比较忙 所以很长时间没有更新技术博客了。 在这段时间学习到了很多以前没有接触过的东西。
    2011-05-05
  • PHP设置Cookie的HTTPONLY属性方法

    PHP设置Cookie的HTTPONLY属性方法

    下面小编就为大家带来一篇PHP设置Cookie的HTTPONLY属性方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • Laravel操作redis和缓存操作详解

    Laravel操作redis和缓存操作详解

    这篇文章主要为大家详细介绍了Laravel操作redis和缓存操作的相关知识,文中的示例代码讲解详细,具有一定的学习和借鉴价值,感兴趣的小伙伴可以跟随小编一起学习游戏
    2023-02-02
  • php文件上传后端处理小技巧

    php文件上传后端处理小技巧

    这篇文章主要为大家详细介绍了php文件上传后端处理小技巧,帮助大家更好的进行文件上传操作,感兴趣的朋友可以参考一下
    2016-05-05
  • PHP简单实现记录网站访问量功能示例

    PHP简单实现记录网站访问量功能示例

    这篇文章主要介绍了PHP简单实现记录网站访问量功能,涉及php针对文件加锁读写及日期时间转换等相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • PHP 程序授权验证开发思路

    PHP 程序授权验证开发思路

    做一套商业程序,如只充许客户只能用于一台服务器,授权验证就很重要了。
    2009-07-07
  • php读取csv数据保存到数组的方法

    php读取csv数据保存到数组的方法

    这篇文章主要介绍了php读取csv数据保存到数组的方法,通过封装的类文件实现这一功能,是对csv文件操作的实用技巧,需要的朋友可以参考下
    2015-01-01
  • PHP+MySQL之Insert Into数据插入用法分析

    PHP+MySQL之Insert Into数据插入用法分析

    这篇文章主要介绍了PHP+MySQL之Insert Into数据插入用法,实例分析了php+mysql基于Insert Into语句实现数据插入的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • PHP生成器(generator)和协程的实现方法详解

    PHP生成器(generator)和协程的实现方法详解

    这篇文章主要介绍了PHP生成器(generator)和协程的实现方法,结合实例形式详细分析了php生成器以及由此延伸出来的协程相关操作技巧与注意事项,需要的朋友可以参考下
    2018-07-07
  • PHP使用pear实现mail发送功能 windows环境下配置pear

    PHP使用pear实现mail发送功能 windows环境下配置pear

    这篇文章主要介绍在 windows环境下如何配置pear,PHP使用pear实现mail发送功能,感兴趣的小伙伴们可以参考一下
    2016-04-04

最新评论