两个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 搜索框提示(自动完成)实例代码
Feb 05 PHP
php中配置文件操作 如config.php文件的读取修改等操作
Jul 07 PHP
php生成略缩图代码
Jul 16 PHP
解析php curl_setopt 函数的相关应用及介绍
Jun 17 PHP
浅析PHP中Collection 类的设计
Jun 21 PHP
如何利用PHP执行.SQL文件
Jul 05 PHP
摘自织梦CMS中的图片处理类
Aug 08 PHP
laravel + vue实现的数据统计绘图(今天、7天、30天数据)
Jul 31 PHP
php常用的工具开发整理
Sep 26 PHP
laravel 修改记住我功能的cookie保存时间的方法
Oct 14 PHP
Thinkphp 3.2框架使用Redis的方法详解
Oct 24 PHP
PHP 对接美团大众点评团购券(门票)的开发步骤
Apr 03 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
社区(php&amp;&amp;mysql)四
2006/10/09 PHP
PHP在不同页面间传递Json数据示例代码
2013/06/08 PHP
提高PHP性能的编码技巧以及性能优化详细解析
2013/08/24 PHP
PHP实现删除非站内外部链接实例代码
2014/06/17 PHP
php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
2016/05/28 PHP
Yii多表联合查询操作详解
2016/06/02 PHP
php实现微信企业转账功能
2018/10/02 PHP
一种JavaScript的设计模式
2006/11/22 Javascript
基于jQuery架构javascript基础体系
2011/01/01 Javascript
JavaScript下通过的XMLHttpRequest发送请求的代码
2011/06/28 Javascript
JavaScript实现选择框按比例拖拉缩放的方法
2015/08/04 Javascript
跟我学习javascript的for循环和for...in循环
2015/11/18 Javascript
基于jquery插件实现拖拽删除图片功能
2020/08/27 Javascript
微信小程序 用户数据解密详细介绍
2017/01/09 Javascript
微信小程序 页面跳转传递值几种方法详解
2017/01/12 Javascript
javascript实现数字配对游戏的实例讲解
2017/12/14 Javascript
node.js用fs.rename强制重命名或移动文件夹的方法
2017/12/27 Javascript
关于vue中watch检测到不到对象属性的变化的解决方法
2018/02/08 Javascript
vue双向绑定及观察者模式详解
2019/03/19 Javascript
Vue3 源码导读(推荐)
2019/10/14 Javascript
从零学python系列之数据处理编程实例(一)
2014/05/22 Python
Python 实现字符串中指定位置插入一个字符
2018/05/02 Python
关于python3中setup.py小概念解析
2019/08/22 Python
python自动发微信监控报警
2019/09/06 Python
python合并多个excel文件的示例
2020/09/23 Python
Python中使用aiohttp模拟服务器出现错误问题及解决方法
2020/10/31 Python
CSS3实现各种图形的示例代码
2016/10/19 HTML / CSS
Marc Jacobs彩妆官网:Marc Jacobs Beauty
2017/07/03 全球购物
大学生自我评价200字(4篇)
2014/09/17 职场文书
2015年小学中秋节活动总结
2015/03/23 职场文书
刑事法律意见书
2015/06/04 职场文书
谢师宴学生致辞
2015/07/27 职场文书
2019数学教师下学期工作总结
2019/06/27 职场文书
深入理解 Golang 的字符串
2022/05/04 Golang
Mysql中常用的join连接方式
2022/05/11 MySQL
CSS 鼠标点击拖拽效果的实现代码
2022/12/24 HTML / CSS