有人可以帮助我将startdate和enddate从函数存储到数组中进行进一步处理。
下面的php代码将给出给定时期内每个月的开始和结束日期。但我无法将这些值放入函数的数组中。
PHP代码:
<?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>";
}
?>
我需要将上面函数的输出放在这样的数组中: 期望的输出:
$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'
答案 0 :(得分:1)
请在函数
中使用以下脚本$result = array_merge($start_dates_arr, $end_dates_arr);
//$result['start_dates'][] = $end_dates_arr;
return $result;
答案 1 :(得分:0)
您想要的评论是合并两个数组
$new_res = array_merge($res['start_dates'],$res['end_dates']);
print_r($new_res);