doutonghang2761 2016-10-01 11:25
浏览 33
已采纳

在PHP中组合日期范围的算法

I have an array that have start and end date in key/value. eg.

/* formate 'mm/dd/yyyy'=>  'mm/dd/yyyy'*/

$arr = array(
'01/01/2016'=>'01/01/2016',
'01/02/2016'=>'01/02/2016',
'01/03/2016'=>'01/03/2016',


'04/10/2016'=>'04/10/2016',
'04/11/2016'=>'04/11/2016',
'04/12/2016'=>'04/12/2016',


'04/25/2016'=>'04/25/2016',

'04/30/2016'=>'04/30/2016',
'05/01/2016'=>'05/01/2016',
'05/02/2016'=>'05/02/2016' }

)

Here you can see some element have continuously dates. eg. first three element have 04/01 to 04/03 dates. I want that in one element. so new array should be like this >

$arr = array(
    '01/01/2016'=>'01/03/2016',

    '04/10/2016'=>'04/12/2016',

    '04/25/2016'=>'04/25/2016',

    '04/30/2016'=>'05/02/2016'
})

How can do it ?

Thanks

  • 写回答

2条回答 默认 最新

  • dongsu2807 2016-10-01 13:11
    关注

    I know there's already an answer but here's my version - spent a bit of time on it so wanted to share!

    As in your example, I'm assuming your original array is in date order and the keys and values are always the same.

    You can use a function to iterate over your original dataset and return an array of grouped dates as follows...

    function group_dates($dates_array) {
    
       // prepare results array
       $result = array();
    
       // take the first date from the submitted array
       $group_start = array_shift($dates_array);
       $timestamp_previous_iteration = strtotime($group_start);
    
       // iterate over remaining values
       foreach ($dates_array as $date) {
          // calculate current iteration's timestamp
          $timestamp_current = strtotime($date);
    
          // calculate timestamp for 1 day before the current value
          $timestamp_yesterday = strtotime('-1 day', $timestamp_current);
    
          if ($timestamp_previous_iteration != $timestamp_yesterday) {
             // create group...
             $result[$group_start] = date('m/d/Y', $timestamp_previous_iteration);
             // ...and store the next group's start date
             $group_start = $date;
          }
    
          // log timestamp for next iteration
          $timestamp_previous_iteration = $timestamp_current;
       }
    
       // add final group
       $result[$group_start] = date('m/d/Y', $timestamp_previous_iteration);
    
       return $result;
    }
    

    You can then use the function as follows,

    $result_array = group_dates($arr);
    

    Feeding the function the array in your example will result in an array as you requested.

    As your dates are formatted MM/DD/YYYY the string will be correctly converted to a unix timestamp by strtotime(). If you were using other date formats you will want to look at the PHP DateTime object.

    Reference
    http://php.net/manual/en/function.strtotime.php
    http://php.net/manual/en/book.datetime.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?