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);

 

结果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值