查找连载日期连续的时间段在PHP
问题描述:
我有以下阵列日期在PHP中:查找连载日期连续的时间段在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年2月22日2017年2月23日至
和
2017年4月3日至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循环(Demo)
foreach($dates as $date){
if(!isset($start_date)){
$start_date=$date; // temporarily store new start date
}elseif($date==date("Y-m-d",strtotime("$start_date +1 day")) || (isset($end_date) && $date==date("Y-m-d",strtotime("$end_date +1 day")))){
$end_date=$date; // temporarily store new or overwrite existing end date
}else{
$result[]="$start_date to ".(!isset($end_date)?$start_date:$end_date); // current date is not in group, move temporary dates to result array
$start_date=$date; // overwrite previous start date
unset($end_date); // destroy previous end date
}
}
$result[]="$start_date to ".(!isset($end_date)?$start_date:$end_date); // move temporary dates to result array
var_export($result);
方法#2:循环嵌套(Demo)
$copy=$dates; // make a copy in case $dates is to be used again later
while($copy){ // iterate until the array is empty
while(!isset($range) || current($copy)==date("Y-m-d",strtotime(substr($range,-10)." +1 day"))){ // iterate while date is new or consecutive
$range=(!isset($range)?'':substr($range,0,10).' to ').array_shift($copy); // temporarily store/overwrite the range data
}
$result[]=(strlen($range)==10?"$range to $range":$range); // permanently store range data
unset($range); // destroy range string, for next iteration
}
var_export($result);
方法#3:两个foreach循环(Demo)
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);
或者从在方法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对象,以便更容易检查。)如果是,那么您仍然有一个连续的时间段,如果不是,那么您将处于新的开始时间。 – CBroe
也许有趣(重复)? :[算法来组合/合并日期范围](http://stackoverflow.com/questions/4972552/algorithm-to-combine-merge-date-ranges) –