Eddie D. Eddie D. - 7 months ago 11
PHP Question

is it possible to break array like this?

$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


The point is to pass first and last date and make a range

in the end get an array like this

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
)


i got the first array with this, now the problem is being with the range using 4 dates

foreach(array_slice($dates, 2) as $key => $value){
$a[] .= $datas[$key];

if($key == 0){
$a[] .= $value;
} else {
$a[] .= $value;
}
}

Answer

Yes... it is possible and here is what i imagine you want:

            <?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); 
                }
            }
            var_dump($arrArrangedDate);

Dumps:

       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)

I hope this answers your question... ;-)