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

时间:2016-10-01 11:25:17

标签: php arrays algorithm merge date-range

我有一个数组,其键/值的开始和结束日期。 例如。

/* 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' }

)

在这里你可以看到一些元素有连续的日期。 例如。前三个元素有04/01到04/03日期。我希望在一个元素中。所以新数组应该是这样的>

$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'
})

怎么办?

谢谢

3 个答案:

答案 0 :(得分:3)

我知道那里已有答案,但这是我的版本 - 花了一点时间就可以了,所以想分享!

在您的示例中,我假设您的原始数组是按日期顺序排列的,并且键和值始终相同。

您可以使用函数迭代原始数据集并返回分组日期数组,如下所示...

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

然后您可以使用以下功能

$result_array = group_dates($arr);

提供函数,示例中的数组将根据您的请求生成一个数组。

由于您的日期格式为MM/DD/YYYY,因此字符串将被strtotime()正确转换为unix时间戳。如果您使用其他日期格式,则需要查看PHP DateTime对象。

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

答案 1 :(得分:1)

检查以下代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <solid
        android:color="@color/colorAccent"/>
    <size
        android:width="40dp"
        android:height="40dp"/>
</shape>

输出:

<?php

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

    $lastDate = null;
    $ranges = array();
    $currentRange = array();

    foreach ($arr as $date => $value ) {
        $temp = $value;
        $value = date_create(date('m/d/Y', strtotime($value)));

        if (null === $lastDate) {
            $currentRange[] = $temp;
        } else {
            // get the Date object
            $interval = date_diff($value, $lastDate);

            if ($interval->days === 1) {
                // add this date to the current range
                $currentRange[] = $temp;    
            } else {
                // store the old range and start a new
                $ranges[] = $currentRange;
                $currentRange = array($temp);
            }
        } 
        $lastDate = $value;
    }

    $ranges[] = $currentRange;

    $datemerge = array();
    foreach ($ranges as $key) {
        $count = count($key);
        $datemerge[$key[0]] = $key[$count-1];
    }

    print_r($datemerge);

?>

答案 2 :(得分:0)

查看此方法,位于:

React basics

来自: http://mdb-blog.blogspot.com/2020/12/php-merge-dateranges-algorithm.html