两个php日期控制类实例


Posted in PHP onDecember 09, 2014

本文实例讲述了两个php日期控制类。分享给大家供大家参考。具体分析如下:

由于工作需要我找了二个时间日期控制,这个不用js只要php实现的,因为要带参考查询操作,感兴趣的朋友可以参考一下,我自己用的是第二个,所以第二个己作了修改.

实例一,代码如下:

<?php 

class Calendar 

{ 

 var $month; 

 var $year; 

 

    function __construct($year,$month) 

    { 

     $this->year=$year; 

  $this->month=$month; 

 } 

 

 function endday() 

 { 

  $daydate=date("d",mktime(0,0,0,$this->month,35,$this->year)); 

  $endday=35-$daydate; 

  return $endday; 

 } 

 

 function oneday_week() 

 { 

  $oneday_week=date("w",mktime(0,0,0,$this->month,1,$this->year)); 

  return $oneday_week; 

 } 

 

 function title_link() 

 { 

  if(!isset($this->month) && !isset($this->year)) 

  { 

   $this->year  = date("Y"); 

   $this->month = date("m"); 

        } 

   

  $lastmonth=$this->month-1; 

  $nextmonth=$this->month+1; 

  $lastyear=$this->year; 

  $nextyear=$this->year; 

 

        if($this->month <= 1) 

        { 

         $lastmonth=12; 

   $nextmonth=$this->month+1; 

   $lastyear=$this->year-1; 

   $nextyear=$this->year; 

  } 

        elseif ($this->month >= 12)  

        { 

         $lastmonth=$this->month-1; 

         $nextmonth=1; 

   $lastyear=$this->year; 

   $nextyear=$this->year+1; 

        } 

 

        $str ="<td colspan='2'><div align='center'><a href=$PHP_SELF?year=".$lastyear."&month=".$lastmonth."&><<<</a></div></td>"; 

  $str.="<td colspan='3'><div align='center'>".$this->year."--".$this->month."</div></td>"; 

  $str.="<td colspan='2'><div align='center'><a href=$PHP_SELF?year=".$nextyear."&month=".$nextmonth."&>>>></a></div></td>"; 

  return $str; 

 } 

 

 function Show_Calendar() 

 { 

        echo "<table width='181' border=5><tr class='tr.title'>".$this->title_link()."</tr><tr>"; 

        $weekarray=array("日","一","二","三","四","五","六"); 

         

        for($k=0;$k<=6;$k++) 

        { 

         echo "<td><div align='center'>".$weekarray[$k]."</div></td>"; 

        } 

        echo "</tr>"; 

         

        for($i=0;$i<=5;$i++) 

        { 

         echo "<tr>"; 

         for($j=1;$j<=7;$j++) 

         { 

     $math=( $j - $this->oneday_week() ) + 7 * $i; 

     

          echo "<td><div align='center'>"; 

     

          if($math <= $this->endday() and $math>=1) 

     { 

   echo $math; 

     } 

     

         echo "</div></td>"; 

         }     

         echo "</tr>";      

        } 

  echo "</table>"; 

 } 

} 

$calendar=new Calendar($_GET['year'],$_GET['month']); 

$calendar->month=$_GET['month']; 

$calendar->year=$_GET['year']; 

$calendar->Show_Calendar();

?>

实例二,代码如下:
<?php 

header("content-type:text/html;charset=utf-8"); 

?> 

<meta http-equiv="content-type" content="text/html;charset=utf-8"> 

 

<link href="statics/css/zhaoshang.css" rel="stylesheet" type="text/css"> 

<div class="rili"> 

<?php 

if(!emptyempty($_GET)){ 

    $year = isset($_GET['year'])?$_GET['year']:date('y'); 

    $month = isset($_GET['month'])?$_GET['month']:date('m'); 

 $day = isset($_GET['day'])?$_GET['day']:date('d'); 

} 

if(emptyempty($year)){ 

    $year = date('Y'); 

} 

if(emptyempty($month)){ 

    $month = date('m'); 

} 

 

if(emptyempty($month)){ 

    $days = date('d'); 

} 

 

$start_weekday = date('w',mktime(0,0,0,$month,1,$year)); 

//echo $start_weekday; 

$days = date('t',mktime(0,0,0,$month,1,$year)); 

//echo $days; 

$week = array('日','一','二','三','四','五','六'); 

$i = 0; 

$k = 1; 

$j = 0; 

echo '<table >'; 

echo '<tr><td colspan = 7 style ="text-align:center; width:250px; background:#d5e5f6; border-bottom:1px solid #333366;">'.$year.'年'.$month.'月'.'</td></tr>'; 

echo '<tr>'; 

for($i = 0;$i < 7;$i++){ 

    echo '<td>'.$week[$i].'</td>'; 

} 

echo '</tr>'; 

echo '<tr>'; 

for($j = 0;$j < $start_weekday;$j++){ 

    echo '<td style="color:#f1f8fc;">'.$j.'</td>'; 

} 

while($k <= $days){ 

    if($k == $day){ 

        echo '<td style="color:red"><a href="?year='.$year.'&month='.$month.'&day='.$k.'"><span style="color:red">'.$k.'</span></a></td>'; 

    }else{ 

        echo '<td><a href="?year='.$year.'&month='.$month.'&day='.$k.'">'.$k.'</a></td>'; 

    } 

    if(($j+1) % 7 == 0){ 

        echo '</tr><tr>'; 

    } 

    $j++; 

    $k++; 

} 

while($j % 7 != 0){ 

    echo '<td style="color:#f1f8fc;">'.$j.'</td>'; 

    $j++; 

} 

echo '</tr>'; 

 

echo '<tr style="background:#d5e5f6;">'; 

echo "<td><a href=?".lastYear($year,$month).">".'<<'.'</a></td>'; 

echo "<td><a href=?".lastMonth($year,$month).">".'<'.'</a></td>'; 

echo '<td colspan = 3 style="width:105px;" >'; 

echo '<form name = "myform" method = "GET">'; 

echo '<select name = year  class="year">'; 

for($start_year = 1970;$start_year<2039;$start_year++){ 

 $selected=''; 

 if( $year== $start_year ) { $selected='selected'; } 

 echo '<option value ='. $start_year.' '.$selected.'>'.$start_year.'</option>'." nt"; 

} 

echo '</select>'; 

echo '<select name = month class="month">'; 

for($start_month = 1;$start_month<=12;$start_month++){ 

 $selected=''; 

 if( $month== $start_month ) { $selected='selected';} 

 echo '<option value = '.$start_month.' '.$selected.'>'.$start_month.'</option>'." nt"; 

} 

echo '</select>'; 

/*echo '月';*/ 

/*echo '<input type = "submit" name = "search" value = "查询">';*/ 

echo '</form>'; 

echo '</td>'; 

echo "<td><a href=?".nextYear($year,$month).">".'>>'.'</a></td>'; 

echo "<td><a href=?".nextMonth($year,$month).">".'>'.'</a></td>'; 

echo '</tr>'; 

echo '</table>'; 

 

function lastYear($year,$month){ 

 $year = $year-1; 

 return "year=$year&month=$month"; 

} 

function lastMonth($year,$month){ 

 if($month == 1){ 

  $year = $year -1; 

  $month = 12; 

 }else{ 

  $month--; 

 } 

 return "year=$year&month=$month"; 

} 

function nextYear($year,$month){ 

 $year = $year+1; 

 return "year=$year&month=$month"; 

} 

function nextMonth($year,$month){ 

 if($month == 12){ 

  $year = $year +1; 

  $month = 1; 

 }else { 

  $month++; 

 } 

 return "year=$year&month=$month"; 

} 

 

?> 

</div>

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHP 文章中的远程图片采集到本地的代码
Jul 30 PHP
PHP中输出转义JavaScript代码的实现代码
Apr 22 PHP
php开启安全模式后禁用的函数集合
Jun 26 PHP
PHP 万年历实现代码
Oct 18 PHP
php网页标题中文乱码的有效解决方法
Mar 05 PHP
PHP基于imap获取邮件实例
Nov 11 PHP
php带抄送和密件抄送的邮件发送方法
Mar 20 PHP
PHP经典面试题之设计模式(经常遇到)
Oct 15 PHP
PHP使用递归算法无限遍历数组示例
Jan 13 PHP
PHP删除数组中特定元素的两种方法
Feb 28 PHP
php面向对象基础详解【星际争霸游戏案例】
Jan 23 PHP
PHP基于phpqrcode类生成二维码的方法示例详解
Aug 07 PHP
php中Ctype函数用法详解
Dec 09 #PHP
完美解决thinkphp验证码出错无法显示的方法
Dec 09 #PHP
thinkphp缓存技术详解
Dec 09 #PHP
ThinkPHP中url隐藏入口文件后接收alipay传值的方法
Dec 09 #PHP
php生成shtml类用法实例
Dec 09 #PHP
thinkphp文件处理类Dir.class.php的用法分析
Dec 08 #PHP
php中adodbzip类实例
Dec 08 #PHP
You might like
利用PHPExcel读取Excel的数据和导出数据到Excel
2017/05/12 PHP
php 二维数组快速排序算法的实现代码
2017/10/17 PHP
php5与php7的区别点总结
2019/10/11 PHP
jquery限制输入字数,并提示剩余字数实现代码
2012/12/24 Javascript
使用jquery实现图文切换效果另加特效
2013/01/20 Javascript
jquery mobile changepage的三种传参方法介绍
2013/09/13 Javascript
JavaScript中常见获取元素的方法汇总
2015/03/04 Javascript
浅谈Node.js中的定时器
2015/06/18 Javascript
jquery单击事件和双击事件冲突解决方案
2016/03/02 Javascript
jQuery动态移除和添加背景图片的方法详解
2017/03/07 Javascript
easyUI下拉列表点击事件使用方法
2017/05/18 Javascript
JS switch判断 三目运算 while 及 属性操作代码
2017/09/03 Javascript
基于rollup的组件库打包体积优化小结
2018/06/18 Javascript
解决vue attr取不到属性值的问题
2018/09/18 Javascript
JQuery Ajax跨域调用和非跨域调用问题实例分析
2019/04/16 jQuery
[01:10]DOTA2亚洲邀请赛 征战号角响彻全场
2015/01/06 DOTA
[02:43]2018DOTA2亚洲邀请赛主赛事首日TOP5
2018/04/04 DOTA
Python 基于Twisted框架的文件夹网络传输源码
2016/08/28 Python
Python基础教程之利用期物处理并发
2018/03/29 Python
Python多重继承的方法解析执行顺序实例分析
2018/05/26 Python
解决PyCharm同目录下导入模块会报错的问题
2018/10/13 Python
利用Python半自动化生成Nessus报告的方法
2019/03/19 Python
使用python搭建服务器并实现Android端与之通信的方法
2019/06/28 Python
python实现列表的排序方法分享
2019/07/01 Python
HTML5 3D书本翻页动画的实现示例
2019/08/28 HTML / CSS
精彩的大学生自我评价
2013/11/17 职场文书
自立自强的名人事例
2014/02/10 职场文书
商业房地产广告语
2014/03/13 职场文书
党员干部公开承诺书
2014/03/26 职场文书
文化建设工作方案
2014/05/12 职场文书
小学节能减排倡议书
2014/05/15 职场文书
企业活动策划方案
2014/06/02 职场文书
民事上诉状范文
2015/05/22 职场文书
Python实现生成bmp图像的方法
2021/06/13 Python
PostGIS的安装与入门使用指南
2022/01/18 PostgreSQL
详解Python中的for循环
2022/04/30 Python