PHP实现的日历功能示例

早上好,给您新鲜的问候,温暖的祝福,清晨,美好的开端,祝您今天精神振奋,精力充沛,心情愉快,一切都很好!

本文实例讲述了PHP实现的日历功能。分享给大家供大家参考,具体如下:

<?php
/*
 * Created by PhpStorm.
 * User: admin
 */
header("Content-Type:text/html;charset=utf-8");
date_default_timezone_set("PRC");
$a=@$_GET["a"];
$b=@$_GET["b"];
if($a){
  $a=$a;
}else{
  $a=date('y');
}
if($b){
  $b=$b;
}else{
  $b=date('m');
}
$noe=mktime(0,0,0,$b,1,$a); //获取当前的月的一号
$year=date("Y",$noe); //当前的年
$month=date("m",$noe); //当前的月
$week=date("w",$noe); // 每个月的一号是星期几
$days=date("t",$noe); //每个月的总天数
$day=date("d"); //获取今天是几号
$as=$year-1; //获取上一年的年
$bs=$month-1; //获取上个月
$bs=$month+1; // 获取下个月
$as=$year+1; //获取下一年
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>日历</title>
  <style>
    table{
      border: 1px solid #050;
    }
    table th{
      background:#000;
      color:#fff;
      border: 2px solid #050;
    }
  </style>
</head>
<body>
 <table cellpadding="0" cellspacing="0">
   <tr>
     <th><a rel="nofollow noopener noreferrer" href="?a=<?php echo $as; ?>" rel="external nofollow" rel="external nofollow" ><<上一年</a></th>
     <th><a rel="nofollow noopener noreferrer" href="?b=<?php echo $bs; ?>" rel="external nofollow" rel="external nofollow" ><<上个月</a></th>
     <th><?php echo $year."-".$month."-".$day ?></th>
     <th><a rel="nofollow noopener noreferrer" href="?b=<?php echo $bs; ?>" rel="external nofollow" rel="external nofollow" >下个月>></a></th>
     <th><a rel="nofollow noopener noreferrer" href="?a=<?php echo $as; ?>" 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></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>

运行结果:

PS:这里再为大家推荐几款时间及日期相关工具供大家参考:

在线日期/天数计算器:
http://tools.haodaima.com/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.haodaima.com/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.haodaima.com/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具:
http://tools.haodaima.com/code/unixtime

希望本文所述对大家PHP程序设计有所帮助。

到此这篇关于PHP实现的日历功能示例就介绍到这了。人生也是一样,难免遭遇坎坷,有时苦难和不幸就像无边黑夜笼罩着你,这时,你就要为自己点一盏灯,不是拿在手上,而是亮在心里。更多相关PHP实现的日历功能示例内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
PHP性能优化案例分享

PHP实现短信验证码的发送次数限制

PHP中的异常处理机制深入讲解

PHP常见七种算法合集代码实例

PHP微信扫描二维码关注公众号后自动登录第三方网站