php的数学题

我有一个数组,里面存的每一个都是一个年月日的日期的时间戳,
类似如下

$data_arr = array(
    strtotime("2015-08-01"),
    strtotime("2015-08-02"),
    strtotime("2015-08-03"),
    strtotime("2015-08-04"),
    strtotime("2015-08-05"),
    strtotime("2015-08-07"),
    strtotime("2015-08-09"),
    strtotime("2015-08-10"),
    strtotime("2015-08-13")
);

这里面存放的是一个用户某一天没有交费。
现在希望能展示成

2015-08-01 到 2015-08-05 未交费
2015-08-07 未交费
2015-08-09 到 2015-08-10 未交费
2015-08-13 未交费

类似的就是这个意思,
请问应该如何处理数组呢?


慕容森
浏览 852回答 2
2回答

慕无忌1623718

你的问题是一个数组里面保存着一些时间戳,你需要格式化一下这些数据对吧。我建议的方案:首先把时间戳转为具体日期年月日,分割年份,月份,日期出来组装好一个多维数组;然后日期间对比是否相邻,月份和年份对比则是最后一天和最早一天是否相邻,符合相邻条件的把数组连接起来组成新的数组,大概就这么一个思路,由于存在不同年不同月的情况,所以可能会复杂一点,大概需要写一个二次遍历才行,希望能帮到你…

一只甜甜圈

<?php$data_arr = array(&nbsp; &nbsp; strtotime("2015-08-01"),&nbsp; &nbsp; strtotime("2015-08-03"),&nbsp; &nbsp; strtotime("2015-08-04"),&nbsp; &nbsp; strtotime("2015-08-05"),&nbsp; &nbsp; strtotime("2015-08-06"),&nbsp; &nbsp; strtotime("2015-08-07"),&nbsp; &nbsp; strtotime("2015-08-09"),&nbsp; &nbsp; strtotime("2015-08-10"),&nbsp; &nbsp; strtotime("2015-08-13"),&nbsp; &nbsp; strtotime("2015-08-14"),&nbsp; &nbsp; strtotime("2015-08-15"),&nbsp; &nbsp; strtotime("2015-08-19"),&nbsp; &nbsp; strtotime("2015-08-20"),&nbsp; &nbsp; strtotime("2015-08-22"),);array_push($data_arr, null);&nbsp; &nbsp; &nbsp; &nbsp; $start=null;&nbsp; &nbsp; &nbsp; &nbsp; for($i=1;$i<count($data_arr);$i++){&nbsp; &nbsp; &nbsp; &nbsp; if($data_arr[$i]==$data_arr[$i-1]+24*3600){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!$start){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $start=$data_arr[$i-1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($start){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo date("Y-m-d",$start)."到".date("Y-m-d",$data_arr[$i-1])."未付款\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo date("Y-m-d",$data_arr[$i-1])."未付款\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $start=null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }?>
打开App,查看更多内容
随时随地看视频慕课网APP