$a = array('2016-05-06', '2016-05-08', '2016-05-20', '2016-05-23');
foreach($a as $key => $value){
$dateFrom = $value //2016-05-06 Then 2016-05-20
$dateTo = $value //2016-05-08 Then 2016-05-23
function range($dateFrom, $dateTo);
} //in the second loop get the others 2 like above
重点是传递第一个和最后一个日期并制作一个范围
最终获得这样的数组
Array
(
[0] => 2016-05-06
[1] => 2016-05-07
[2] => 2016-05-08
[3] => 2016-05-20
[4] => 2016-05-21
[5] => 2016-05-22
[6] => 2016-05-23
)
我得到了第一个数组,现在问题在于使用4个日期的范围
foreach(array_slice($dates, 2) as $key => $value){
$a[] .= $datas[$key];
if($key == 0){
$a[] .= $value;
} else {
$a[] .= $value;
}
}
答案 0 :(得分:2)
使用DatePeriods执行此操作的一种方法:
$ranges = array_chunk($dates, 2); // Divide the array into groups of two
$interval = new DateInterval('P1D'); // Define an interval of one day
foreach ($ranges as $range) {
// Create DateTime objects for the start and end dates
$start = new DateTime($range[0]);
$end = new DateTime($range[1]);
$end->add($interval);
// Create a new DatePeriod object using the start and end DateTime objects
$period = new DatePeriod($start, $interval, $end);
// Iterate over the DatePeriod to fill your result array
foreach ($period as $date) {
$result[] = $date->format('Y-m-d');
}
}
答案 1 :(得分:0)
是的......这是可能的,这就是我想象你想要的:
<?php
$a = array('2016-05-06', '2016-05-08', '2016-05-20', '2016-05-23');
$arrDuos = array();
$arrTempRange = array();
foreach($a as $intKey=>$dateVal){
$arrTempRange[] = $dateVal;
if($intKey != 0 && $intKey%2){
$arrDuos[] = $arrTempRange;
$arrTempRange = array();
}
}
$arrArrangedDate = array();
foreach($arrDuos as $intKey=>$arrDateRange){
$startDate = $arrDateRange[0];
$endDate = $arrDateRange[1];
$dateDiff = date_diff( date_create($endDate), date_create($startDate) )->days;
for($i = 0; $i<=$dateDiff; $i++){
$incrementedDate = strtotime($startDate) + (60*60*24*$i);
$arrArrangedDate[] = date("Y-m-d", $incrementedDate);
}
}
// MANUALLY APPEND THE LAST ELEMENT OF THE ARRAY TO THE
// RESULTANT ARRAY IF THE ARRAY LENGTH IS ODD LIKE SO:
if(count($a)%2){
$arrArrangedDate[] = end($a);
}
var_dump($arrArrangedDate);
转储:
array (size=7)
0 => string '2016-05-06' (length=10)
1 => string '2016-05-07' (length=10)
2 => string '2016-05-08' (length=10)
3 => string '2016-05-20' (length=10)
4 => string '2016-05-21' (length=10)
5 => string '2016-05-22' (length=10)
6 => string '2016-05-23' (length=10)
我希望这能回答你的问题...; - )