質問

Can someone help me in storing startdate and enddate from a function to an array for further processing.

Below php code will give start and end dates of each month for a given period. But I am unable to put these values into an array from the function.

PHP code:

    <?php
    //Function to return out start and end dates of all months in a date range given

    function rent_range($start_date, $end_date)
    {
        $start_date = date("m/d/Y", strtotime($start_date));
        $end_date   = date("m/d/Y", strtotime($end_date));
        $start = strtotime($start_date);
        $end = strtotime($end_date);

        $month = $start;
        $months[] = date('Y-m', $start);
        while($month < $end) {
          $month = strtotime("+1 month", $month);
          $months[] = date('Y-m', $month);
        }

        foreach($months as $mon)
        {
            $mon_arr = explode( "-", $mon);
            $y = $mon_arr[0];
            $m = $mon_arr[1];
            $start_dates_arr[] = date("m/d/Y", strtotime($m.'/01/'.$y.' 00:00:00'));
            $end_dates_arr[] = date("m/d/Y", strtotime('-1 minute', strtotime('+1 month',strtotime($m.'/01/'.$y.' 00:00:00'))));
        }

        //to remove first month in start date and add our start date as first date
        array_shift($start_dates_arr);
        array_pop($start_dates_arr);
        array_unshift($start_dates_arr, $start_date);

        //To remove last month in end date and add our end date as last date
        array_pop($end_dates_arr);
        array_pop($end_dates_arr);
        array_push($end_dates_arr, $end_date);

        $result['start_dates'] = $start_dates_arr;
        $result['end_dates'] = $end_dates_arr;
        return $result;
    }

    $start_date = '2013-04-01';
    $end_date = '2014-03-31';
    $res = rent_range($start_date, $end_date);
    echo "<pre>";
    print_r($res);
    echo "</pre>";

    for($i=0; $i<11; $i++)
    {
    echo $res[$i]."<br>";
    }
    ?>

I need the output of the above function to be in an array like this: Desired output:

    $res[0]='04/01/2013'
    $res[1]='05/01/2013'
    -
    -
    -
    -
    -
    -
    $res[11]='03/01/2014'

    $res[12]='04/30/2013'
    $res[13]='05/31/2013'
    -
    -
    -
    -
    $res[24]='03/31/2014'
役に立ちましたか?

解決 2

From your comment you want is to merge both your array,

$new_res = array_merge($res['start_dates'],$res['end_dates']);
print_r($new_res);

Demo.

他のヒント

Please use below scripts in the function

$result = array_merge($start_dates_arr, $end_dates_arr);
//$result['start_dates'][] = $end_dates_arr;
return $result;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top