无为清净楼资源网 Design By www.qnjia.com

最近的一个项目中,需要将数据用日历方式显示,网上有很多的JS插件,后面为了自己能有更大的控制权,决定自己制作一个日历显示。如下图所示:

教大家制作简单的php日历

一、计算数据
1、new一个Calendar类

2、初始化两个下拉框中的数据,年份与月份

3、初始化要搜索的年份和月份

4、计算得出日历中每一天的数据信息,包括css、天数

<"htmlcode">
<div style="padding:20px">
  <select name="ddlYear">
  <"<" <"selected"'"ddlMonth">
  <"<" <"selected"'"submit" value="修改"/>
 </div>
 <table width="100%" cellspacing="0" class="table_calendar">
  <thead class="f14">
    <tr>
     <td width="16%">日</td>
     <td width="14%">一</td>
     <td width="14%">二</td>
     <td width="14%">三</td>
     <td width="14%">四</td>
     <td width="14%">五</td>
     <td width="14%">六</td>
    </tr>
  </thead>
  <tbody class="f14">
   <"<">
      <p class="<"><"htmlcode">
/**
  * @deprecated 生成日历的各个边界值
  * @param string $year
  * @param string $month
  * @return array
  */
 function threshold($year, $month) {
  $firstDay = mktime(0, 0, 0, $month, 1, $year);
  $lastDay = strtotime('+1 month -1 day', $firstDay);
  //取得天数 
  $days = date("t", $firstDay);
  //取得第一天是星期几
  $firstDayOfWeek = date("N", $firstDay);
  //获得最后一天是星期几
  $lastDayOfWeek = date('N', $lastDay);
  
  //上一个月最后一天
  $lastMonthDate = strtotime('-1 day', $firstDay);
  $lastMonthOfLastDay = date('d', $lastMonthDate);
  //下一个月第一天
  $nextMonthDate = strtotime('+1 day', $lastDay);
  $nextMonthOfFirstDay = strtotime('+1 day', $lastDay);
  
  //日历的第一个日期
  if($firstDayOfWeek == 7)
   $firstDate = $firstDay;
  else 
   $firstDate = strtotime('-'. $firstDayOfWeek .' day', $firstDay);
  //日历的最后一个日期
  if($lastDayOfWeek == 6)
   $lastDate = $lastDay;
  elseif($lastDayOfWeek == 7) 
   $lastDate = strtotime('+6 day', $lastDay);
  else
   $lastDate = strtotime('+'.(6-$lastDayOfWeek).' day', $lastDay);
  
  return array(
    'days' => $days, 
    'firstDayOfWeek' => $firstDayOfWeek, 
    'lastDayOfWeek' => $lastDayOfWeek,
    'lastMonthOfLastDay' => $lastMonthOfLastDay,
    'firstDate' => $firstDate,
    'lastDate' => $lastDate,
    'year' => $year,
    'month' => $month
  );
 }

2、caculate方法,计算日历的天数与样式

  1)将上个月的天数计算出来,本月第一天的星期不是星期天的话,就需要根据上个月的最后一天计算

  2)将本月的天数遍历出来,如果是休息天就加上特殊的css样式

  3)将下个月的天数计算出来,分三种情况,星期日、星期六和工作日
  

/**
  * @author Pwstrick
   * @param array $calendar 通过threshold方法计算后的数据
  * @deprecated 计算日历的天数与样式
  */
 function caculate($calendar) {
  $days = $calendar['days'];
  $firstDayOfWeek = $calendar['firstDayOfWeek'];//本月第一天的星期
  $lastDayOfWeek = $calendar['lastDayOfWeek'];//本月最后一天的星期
  $lastMonthOfLastDay = $calendar['lastMonthOfLastDay'];//上个月的最后一天
  $year = $calendar['year'];
  $month = $calendar['month'];
  
  $dates = array();
  if($firstDayOfWeek != 7) {
   $lastDays = array();
   $current = $lastMonthOfLastDay;//上个月的最后一天
   for ($i = 0; $i < $firstDayOfWeek; $i++) {
    array_push($lastDays, $current);//添加上一个月的日期天数
    $current--;
   }
   $lastDays = array_reverse($lastDays);//反序
   foreach ($lastDays as $index => $day) {
    array_push($dates, array('day' => $day, 'tdclass' => ($index ==0 "htmlcode">
 /**
  * @author Pwstrick
  * @param array $caculate 通过caculate方法计算后的数据
  * @deprecated 画表格,设置table中的tr与td
  */
 function draw($caculate) {
  $tr = array();
  $length = count($caculate);
  $result = array();
  foreach ($caculate as $index => $date) {
   if($index % 7 == 0) {//第一列
    $tr = array($date);
   }elseif($index % 7 == 6 || $index == ($length-1)) {
    array_push($tr, $date);
    array_push($result, $tr);//添加到返回的数据中
    $tr = array();//清空数组列表
   }else {
    array_push($tr, $date);
   }
  }
  return $result;
 }

通过本文大家应该知道日历制作的方法了,那就趁热打铁,做一个属于自己日历。

附源码:教大家制作简单的php日历

标签:
php简单教程,php日历,php日历代码,php日历插件

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?