PHP 日历练习

<?php
/*
 * Created by PhpStorm.
 * User: admin
 */
header("Content-Type:text/html;charset=utf-8");
date_default_timezone_set("PRC");
$year = @$_GET["year"];
$month=@$_GET["month"];
if($year){
  $year=$year;
}else{
  $year=date('y');
}
if($month){
  $month=$month;
}else{
  $month=date('m');
}
$noe_day=mktime(0,0,0,$month,1,$year); //获取当前的月的一号
$year_this=date("Y",$noe_day); //当前的年
$month_this=date("m",$noe_day); //当前的月
$week=date("w",$noe_day); // 每个月的一号是星期几
$days=date("t",$noe_day); //每个月的总天数
$day=date("d"); //获取今天是几号
$years=$year_this-1; //获取上一年的年
$months=$month_this-1; //获取上个月
$months=$month_this+1; // 获取下个月
$years=$year_this+1; //获取下一年

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>日历</title>
</head>
<body>
 <table cellpadding="0" cellspacing="0" align="center" border="1">
   <tr>
     <th><a href="?a=<?php echo $years; ?>" rel="external nofollow" rel="external nofollow" ><<上一年</a></th>
     <th><a href="?b=<?php echo $months; ?>" rel="external nofollow" rel="external nofollow" ><<上个月</a></th>
     <th><?php echo $year."-".$month."-".$day ?></th>
     <th><a href="?b=<?php echo $months; ?>" rel="external nofollow" rel="external nofollow" >下个月>></a></th>
     <th><a href="?a=<?php echo $years; ?>" rel="external nofollow" rel="external nofollow" >下一年>></a></th>
   </tr>
   <tr>
     <th>星期日</th>
     <th>星期一</th>
     <th>星期二</th>
     <th>星期三</th>
     <th>星期四</th>
     <th>星期五</th>
     <th>星期六</th>
   </tr>
  <tr>
    <?php
    for($i=0;$i<$week;$i++){
        echo "<td>&nbsp;</td>"; //获取当月一号前面的空格
    }
    for($k=1;$k<=$days;$k++){
        if($k==$day){
            echo "<th>".$k."</th>"; //输出今天是几号
        }else{
            echo "<td>".$k."</td>"; //输出当月天数
        }
        if(($k+$week)%7==0){
            echo "<tr></tr>"; // 一周七天换行
        }
    }
    ?>
  </tr>
 </table>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值