两个php日期控制类实例
程序员文章站
2023-11-03 15:26:33
本文实例讲述了两个php日期控制类。分享给大家供大家参考。具体分析如下:
由于工作需要我找了二个时间日期控制,这个不用js只要php实现的,因为要带参考查询操作,感兴趣的...
本文实例讲述了两个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();
?>
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>
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程序设计有所帮助。