两个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程序中的常见漏洞进行攻击(上)
Oct 09 PHP
网站当前的在线人数
Oct 09 PHP
php权重计算方法代码分享
Jan 09 PHP
php实现水仙花数示例分享
Apr 03 PHP
2个自定义的PHP in_array 函数,解决大量数据判断in_array的效率问题
Apr 08 PHP
Codeigniter实现智能裁剪图片的方法
Jun 12 PHP
ThinkPHP模板判断输出Defined标签用法详解
Jun 30 PHP
PHP检测用户语言的方法
Jun 15 PHP
php获取指定数量随机字符串的方法
Feb 06 PHP
PHP异常处理定义与使用方法分析
Jul 25 PHP
php实现socket推送技术的示例
Dec 20 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 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
Windows IIS PHP 5.2 安装与配置方法
2009/06/08 PHP
PHP 程序员的调试技术小结
2009/11/15 PHP
解决PHP在DOS命令行下却无法链接MySQL的技术笔记
2010/12/29 PHP
PHP flock 文件锁详细介绍
2012/12/29 PHP
PHP判断来访是搜索引擎蜘蛛还是普通用户的代码小结
2015/09/14 PHP
Yii使用Captcha验证码的方法
2015/12/28 PHP
PHP调试及性能分析工具Xdebug详解
2017/02/09 PHP
yii2多图上传组件的使用教程
2018/05/10 PHP
老鱼 浅谈javascript面向对象编程
2010/03/04 Javascript
jquery 插件学习(六)
2012/08/06 Javascript
图片img的src不变让浏览器重新加载实现方法
2013/03/29 Javascript
基于jquery的网站幻灯片切换效果焦点图代码
2013/09/15 Javascript
JavaScript网页定位详解
2014/01/13 Javascript
js获取和设置属性的方法
2014/02/20 Javascript
jQuery实现倒计时按钮功能代码分享
2014/09/03 Javascript
JavaScript中的函数嵌套使用
2015/06/04 Javascript
修复jQuery tablesorter无法正确排序的bug(加千分位数字后)
2016/03/30 Javascript
js图片上传的封装代码
2017/08/01 Javascript
JS判断数组那点事
2017/10/10 Javascript
详解JS中统计函数执行次数与执行时间
2018/09/04 Javascript
cdn模式下vue的基本用法详解
2018/10/07 Javascript
微信小程序实现渐入渐出动画效果
2019/06/13 Javascript
微信小程序实现定位及到指定位置导航的示例代码
2019/08/20 Javascript
layui实现根据table数据判断按钮显示情况的方法
2019/09/26 Javascript
vue实现节点增删改功能
2019/09/26 Javascript
JavaScript设计模式之观察者模式与发布订阅模式详解
2020/05/07 Javascript
Python计算三维矢量幅度的方法
2015/06/15 Python
详解Django通用视图中的函数包装
2015/07/21 Python
Python如何爬取实时变化的WebSocket数据的方法
2019/03/09 Python
Flask 上传自定义头像的实例详解
2020/01/09 Python
python tkinter的消息框模块(messagebox,simpledialog)
2020/11/07 Python
深入理解HTML5定时器requestAnimationFrame的使用
2018/12/12 HTML / CSS
Original Penguin英国官方网站:美国著名休闲时装品牌
2016/10/30 全球购物
初三学生语文考试作弊检讨书
2014/12/14 职场文书
nginx配置虚拟主机的详细步骤
2021/07/21 Servers
JavaScript架构搭建前端监控如何采集异常数据
2022/06/25 Javascript