Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php中的序列化日期查找连续时间段_Php_Arrays_Date - Fatal编程技术网

从php中的序列化日期查找连续时间段

从php中的序列化日期查找连续时间段,php,arrays,date,Php,Arrays,Date,我有以下php中带有日期的数组: Array ( [0] => '2017-02-22' [1] => '2017-02-23' [2] => '2017-04-03' [3] => '2017-04-04' [4] => '2017-04-05' [5] => '2017-04-06' ) 有没有办法找到数组中包含的连续日期周期 例如,对于上述阵列,预期结果为: 2017-02-22至2017-02-23 及 2017-04-03

我有以下php中带有日期的数组:

Array ( 
  [0] => '2017-02-22'
  [1] => '2017-02-23'
  [2] => '2017-04-03'
  [3] => '2017-04-04'
  [4] => '2017-04-05'
  [5] => '2017-04-06'
)
有没有办法找到数组中包含的连续日期周期

例如,对于上述阵列,预期结果为:

2017-02-22至2017-02-23

2017-04-03至2017-04-06


谢谢你的帮助

执行此任务的方法有很多。有些比其他更难阅读。我会列举一些。为了提高效率,我建议使用第一种方法,为了便于修改输出,可能使用第三种方法

我修改了
$dates
数组中的示例数据,以显示它适应月份变化和独立日期

以下所有方法都假定日期已按升序排序。如果没有,则
sort()
将在
Y-m-d
格式化日期工作;否则将需要
usort()

我的方法将采用如下日期数组:

$dates=[
  '2017-02-28',
  '2017-03-01',
  '2017-04-03',
  '2017-04-04',
  '2017-04-06',
  '2017-04-08',
  '2017-04-09',
  '2017-04-10'
];
并输出以下内容:

array (
  0 => '2017-02-28 to 2017-03-01',
  1 => '2017-04-03 to 2017-04-04',
  2 => '2017-04-06 to 2017-04-06',
  3 => '2017-04-08 to 2017-04-10',
)
方法#1:单Foreach循环()

方法2:嵌套While循环()

方法#3:两个Foreach循环()

或者从方法3中的
$grouped
数组中,您可以简单地更改输出结构,以包含“范围字符串”作为键,子数组将各个日期作为元素,使用以下方法:

foreach($grouped as $group){
    $result[current($group)." to ".end($group)]=$group;
}
替代输出:

array (
  '2017-02-28 to 2017-03-01' => 
  array (
    0 => '2017-02-28',
    1 => '2017-03-01',
  ),
  '2017-04-03 to 2017-04-04' => 
  array (
    0 => '2017-04-03',
    1 => '2017-04-04',
  ),
  '2017-04-06 to 2017-04-06' => 
  array (
    0 => '2017-04-06',
  ),
  '2017-04-08 to 2017-04-10' => 
  array (
    0 => '2017-04-08',
    1 => '2017-04-09',
    2 => '2017-04-10',
  ),
)

循环检查条目,并检查前一个条目是否比当前条目早一天。(您可能希望用这些数据字符串创建DateTime对象,以便更容易地检查。)如果是,则您仍然有一个连续的周期,如果不是,则您处于新周期的开始。可能有趣(重复)?:
foreach($dates as $date){
    if(!isset($grouped)){  // first group
        $i=0;  // first group, index is zero
    }elseif($date!=date("Y-m-d",strtotime("$date_checker +1 day"))){  // if non-consecutive
        ++$i;  // next group, index is incremented
    }
    $grouped[$i][]=$date_checker=$date;  // store date as temporary date checker and into appropriate group
}

foreach($grouped as $group){
    $result[]=current($group)." to ".end($group);
}
var_export($result);
foreach($grouped as $group){
    $result[current($group)." to ".end($group)]=$group;
}
array (
  '2017-02-28 to 2017-03-01' => 
  array (
    0 => '2017-02-28',
    1 => '2017-03-01',
  ),
  '2017-04-03 to 2017-04-04' => 
  array (
    0 => '2017-04-03',
    1 => '2017-04-04',
  ),
  '2017-04-06 to 2017-04-06' => 
  array (
    0 => '2017-04-06',
  ),
  '2017-04-08 to 2017-04-10' => 
  array (
    0 => '2017-04-08',
    1 => '2017-04-09',
    2 => '2017-04-10',
  ),
)