两个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的FTP学习(一)[转自奥索]
Oct 09 PHP
php 仿Comsenz安装效果代码打包提供下载
May 09 PHP
JavaScript创建命名空间的5种写法
Jun 24 PHP
去掉destoon资讯内容页keywords关键字自带的文章标题的方法
Aug 21 PHP
php将textarea数据提交到mysql出现很多空格的解决方法
Dec 19 PHP
DEDECMS首页调用图片集里的多张图片
Jun 05 PHP
php 时间time与日期date之间的使用详解及区别
Nov 07 PHP
PHP高精确度运算BC函数库实例详解
Aug 15 PHP
微信公众号开发之获取位置信息php代码
Jun 13 PHP
PHP PDOStatement::execute讲解
Jan 31 PHP
PHP Trait代码复用类与多继承实现方法详解
Jun 17 PHP
使用composer安装使用thinkphp6.0框架问题【视频教程】
Oct 01 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
2020最新CPU的性能排名
2020/04/02 数码科技
提问的智慧(2)
2006/10/09 PHP
PHP session常见问题集锦及解决办法总结
2007/03/18 PHP
php addslashes和mysql_real_escape_string
2010/01/24 PHP
《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
2010/11/01 PHP
ThinkPHP3.1新特性之查询条件预处理简介
2014/06/19 PHP
php实现Mongodb自定义方式生成自增ID的方法
2015/03/23 PHP
jQuery 验证插件 Web前端设计模式(asp.net)
2010/10/17 Javascript
Javascript控制页面链接在新窗口打开具体方法
2013/08/16 Javascript
jquery获取tagName再进行判断
2014/05/29 Javascript
AMD异步模块定义介绍和Require.js中使用jQuery及jQuery插件的方法
2014/06/06 Javascript
js判断浏览器是否支持html5
2014/08/17 Javascript
JS实现仿雅虎首页快捷登录入口及导航模块效果
2015/09/19 Javascript
基于jQuery仿淘宝产品图片放大镜代码分享
2020/06/23 Javascript
webpack构建换肤功能的思路详解
2017/11/27 Javascript
详解vue.js移动端配置flexible.js及注意事项
2019/04/10 Javascript
Vue组件基础用法详解
2020/02/05 Javascript
js实现轮播图特效
2020/05/28 Javascript
vue vant中picker组件的使用
2020/11/03 Javascript
[43:18]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.22
2019/09/05 DOTA
Python整型运算之布尔型、标准整型、长整型操作示例
2017/07/21 Python
Python3 queue队列模块详细介绍
2018/01/05 Python
python3.6+django2.0开发一套学员管理系统
2018/03/03 Python
int在python中的含义以及用法
2019/06/27 Python
深入了解Python enumerate和zip
2020/07/16 Python
Canvas与图片压缩的示例代码
2017/11/28 HTML / CSS
后勤部长岗位职责
2013/12/14 职场文书
四年大学生活的自我评价范文
2014/02/07 职场文书
个人四风问题对照检查材料
2014/10/01 职场文书
个人思想政治总结
2015/03/05 职场文书
集团财务总监岗位职责
2015/04/03 职场文书
技术员岗位职责范本
2015/04/11 职场文书
2019年自助餐厅创业计划书模板
2019/08/22 职场文书
Python爬虫爬取全球疫情数据并存储到mysql数据库的步骤
2021/03/29 Python
详解nginx location指令
2022/01/18 Servers
Redis 中使用 list,streams,pub/sub 几种方式实现消息队列的问题
2022/03/16 Redis