zoukankan      html  css  js  c++  java
  • PHP生成日历数组,减少分页

    2016年11月14日 20:45:41 星期一

    情景,

    每个用户设置定投, app的屏幕比较小, 觉得常规的线性分页下拉不是很方便

    吧列表改为日历格式的会更方便执行, 每年12个月, 总过12个页面

    像这种的:

    代码:

     1 $year = '2016';
     2 $list = [];
     3 $index = 1;
     4 for ($month=1; $month<=12; $month++) {
     5     $monthStart = $year.'-'.$month.'-01'; //每月1号
     6     $timestamp = strtotime($monthStart); //时间戳
     7     $daysOfMonth = date('t', $timestamp); //这个月的天数
     8     
     9     //循环当月的每一天
    10     for ($day=1; $day<=$daysOfMonth; $day++) {
    11         $oneDay = $year.'-'.$month.'-'.$day;
    12         $timestamp = strtotime($oneDay); //时间戳
    13         $dayOfWeek = date('N', $timestamp); //周几
    14         
    15         if ($dayOfWeek == 7) {
    16             $list[$month][$index][$dayOfWeek][$day] = '1';
    17             $index++;
    18         } else {
    19             $list[$month][$index][$dayOfWeek][$day] = '1';
    20         }
    21     }
    22 }
    23 
    24 echo json_encode($list);

    结果:

     

  • 相关阅读:
    黄宗禹9.11作业
    黄宗禹第一次作业
    9.11
    9.18
    计算平均速度
    圆的周长与面积
    JAVA 作业
    9.11
    9.25
    计算平均速度题
  • 原文地址:https://www.cnblogs.com/iLoveMyD/p/6063406.html
Copyright © 2011-2022 走看看