您当前的位置: 首页 > 网站编程 > PHP教程 > 使用php制作的日历

使用php制作的日历

作者:不详 来源:网络 发布时间: 2014-08-12 13:45 点击:
这篇文章主要介绍了使用php制作的日历,下面有效果图,大家参考使用吧 calendar.class.php 代码如下: ?php class Calendar { private $year; //当前的年 private $month; //当前的月 private $start_weekday; //当月的第一天对应的是周几 private $days; //当前月一共

使用php制作的日历

  这篇文章主要介绍了使用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制作的日历"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4