首页 > php代码 > PHP简单日历实例

PHP简单日历实例

<?php

/*

 * PHP简单日历实例

 * 作者: 多菜鸟

 * 邮箱: kingerq AT msn DOT com

 * 来源: http://blog.csdn.net/kingerq/

 * 创建时间: 2008-01-03

**/


$date = preg_match("//d{4}/-/d{2}/-/d{2}/", $_GET["YMD"]) ? $_GET["YMD"] : date( "Y-m-d" );

$thisYear = date( "Y", strtotime( $date) );

$thisMonth = date( "m", strtotime( $date) );

$firstDay = date( "w", strtotime( $thisYear."-".$thisMonth."-01" ) );

$monthNum = date( "t", strtotime( $date) );

?>

<html>

<head>

<title>Calendar</title>

<style type="text/css">

.datehead td{ 

  background-color: #669966; 

  border: 1px solid #4F774F; 

  color:#FFFFFF; 

  height:30px; 

  font-size:18px; 

  font-weight:bold; 

  text-align:center;

  font-family: Geneva, Arial, Helvetica, sans-serif;

}

.daynormalbg td{

 background-color:#EEEEEE;

 border:solid #dddddd 1px;

 height:100px;

 vertical-align: top;

 width:14%;

}

.daynormalfont div{

 color:#000000;

 font-weight:bold;

 font-size:18px;

 font-family: Geneva, Arial, Helvetica, sans-serif;

}

.sunday{color:#CC0000 !important; }

.saturday{color: #006600 !important;}

.todaybg{

 background-color:#0099FF !important;

 text-decoration: underline;

}

a {font-family:Geneva, Arial, Helvetica, sans-serif; color:#333333; font-size:12px}

a:hover{text-decoration:none;}

</style>

</head>


<body>

<table border="0" align="center" cellpadding="5" cellspacing="0">

  <tr>

    <td>      

 <p><a href="?YMD=<?=date( "Y-m", strtotime( $date." -1 year") )."-01"?>">&lt;&lt;Last Year</a> 

        <a href="?YMD=<?=date( "Y-m", strtotime( $date." -1 month") )."-01"?>">&lt;Last Month</a> 

        <span class="sunday">

        <?=$date?>

        </span> 

        <a href="?YMD=<?=date( "Y-m", strtotime( $date." +1 month") )."-01"?>">Next Month&gt;</a> 

        <a href="?YMD=<?=date( "Y-m", strtotime( $date." +1 year") )."-01"?>">Next Year&gt;&gt;</a> 

        <a href="?">Today</a> </p></td></tr>

</table>

<table width="95%" border="0" align="center" cellpadding="2" cellspacing="2" class="daynormalfont">

  <tr class="datehead">

    <td><div align="center" class="sunday">SUN</div></td>

    <td>MON</td>

    <td>TUE</td>

    <td>WED</td>

    <td>THU</td>

    <td>FRI</td>

    <td><div align="center" class="saturday">SAT</div></td>

  </tr>

<?php

$i = 1;

$num = 0;

while( 1 ) {//row

?>

  <tr class="daynormalbg">

<?php

  while( $i <= $monthNum ) {//list day

    while( $firstDay ) {//empty in front

?> 

    <td>&nbsp;</td>

 <?php

   $firstDay--;

   $num++;

 }

 $daykey = str_pad($i, 2, 0, STR_PAD_LEFT);

 $thisDay = date("Y-m-d", strtotime($thisYear."-".$thisMonth."-".$daykey) );

 $week = date("w", strtotime($thisDay));

 $className = ( $week%7 == 0 ? " class='sunday'" : ( $week%7 == 6 ? " class='saturday'" : ""));

 ?>

    <td<?=($thisDay == date("Y-m-d") ? ' class="todaybg"' : "" ) ?>><div<?=$className?>><?=$i ?></div>   </td>

<?php

    $i++;

 $num++;

 if( $num % 7 == 0 ) break;

  }

  while( $num % 7 != 0 ) {//list empty cell

 ?>

    <td>&nbsp;</td>

<?php

    $num++;

  }

?> 

  </tr>

<?php

  if( $i >= $monthNum ) break;

}

?> 

  <tr class="datehead">

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

  </tr>

</table>

</body>

</html> 


永久地址:http://www.phprm.com/code/4aad3913be6cde02e38343af0f87fcb9.html

转载随意~请带上教程地址吧^^

标签:none

发表留言