程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php實現的日歷程序

php實現的日歷程序

編輯:PHP綜合

本文實例講述了php實現的日歷程序。分享給大家供大家參考。具體如下:

<?php
////判斷$Year和$Month是否為空,如果為空就獲取系統當前日期,否則用$_GET獲取
$Year = empty($_GET['Year']) ? date("Y"): $_GET["Year"];
$Month = empty($_GET['Month']) ? date("m"): $_GET["Month"];
////獲取當前日期////
$Today = date("Ymj");
////初始化日期數組////
$Maxday = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31");
$Maxday_count = count($Maxday);
////初始化星期數組////
$Weekday = array("<font color='red'>Sun</font>","Mon","Tue","Wed","Thu","Fri","<font color='red'>Sat</font>");
$Weekday_count = count($Weekday);
$daynum = 0;
$space = "<td> </td>";
$col = 0;
////打印表格頭部////
?>
<form action="calendar.php" method="post" name="linked">
<table><tr><td colspan=3 align=center><a href="?Year=<?php if($Year-1 > 1900)echo $Year-1;else echo $Year;?>&Month=<?php echo $Month?>" onClick="javascript:document.linked.submit();" name="Year"><<</a> <?php echo $Year?> <a href="?Year=<?php if($Year+1 <2999) echo $Year+1;else echo $Year;?>&Month=<?php echo $Month?>" onClick="javascript:document.linked.submit();" name="Year">>></a></td>
<td> </td><td colspan=3 align=center><a href="?Year=<?php echo $Year?>&Month=<?php if($Month-1 >0) if($Month-1 < 10)echo "0".($Month-1); else echo $Month-1; else echo $Month;?>" onClick="javascript:document.linked.submit();" name="Month"><<</a> <?php echo $Month;?> <a href="?Year=<?php echo $Year?>&Month=<?php if($Month+1 <13) if($Month+1 <10) echo "0".($Month+1);else echo ($Month+1); else echo $Month;?>" onClick="javascript:document.linked.submit();" name="Month">>></a></td></tr><tr><td colspan="8"></td></tr><tr>
<?php
////打印星期列表////
while(list(,$Weekvalue) = each($Weekday))
 echo "<td align=center> ".$Weekvalue." </td>";
echo "</tr>";
////打印日期列表////
//當月份為4,6,9,11的時候,從數組末尾刪除一個值,也就是刪除31這個值.
if($Month == 4 or $Month == 6 or $Month == 9 or $Month == 11)
{
 array_pop($Maxday);
 $Maxday_count--;
}
//先判斷月份是否為2月,如果是的話再判斷年份是否為閏年,如果是的話就從數組尾部移除2個值,否則就移除3個值.
if($Month == 2)
{
 if($Year % 4 == 0)
 {
 array_pop($Maxday);
 array_pop($Maxday);
 $Maxday_count-=2;
 }
 else
 {
 array_pop($Maxday);
 array_pop($Maxday);
 array_pop($Maxday);
 $Maxday_count-=3;
 }
}
////打印日期////
while($daynum < $Maxday_count)
{
echo "<tr>";
 while(list($colnum,$Dayvalue) = each($Maxday))
 {
  //判斷當前日期為一星期中的哪一天
  $DayWeek = date("w",strtotime($Year."-".$Month."-".$Dayvalue));
  //當當前日期處於第1個,也就是每個月的第1天的時候,在它前面補上相應的空格子
  if($Dayvalue == 1)
   for($i=0;$i<$DayWeek;$i++)
   echo $space;//打印每個月空出的幾個格子
  $daynum++;
  //當打印日期的時候與當前日期字符串進行比較,如果相等就將字體標為紅色.
  if($Year.$Month.$Dayvalue == $Today)
  {
   $u1 = "<b><u>";
   $u2 = "</b></u>";
  }
  else
  {
   $u1 = "";
   $u2 = "";
  }
  if($DayWeek == 0 or $DayWeek == 6)
   $color = "red";
  else
   $color = "black";
  echo "<td align=center><font color=".$color."> ".$u1.$Dayvalue.$u2." </font></td>";
  //用本周第幾天這個值加1,如果是7的倍數就換行退出此輪循環(換行打印)
  if(($DayWeek+1) % 7 == 0)
  {
   $col++;
   break;
  }
 }
echo "</tr>";
}
?>

運行效果如下:

希望本文所述對大家的php程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved