有人可以幫助我將startdate和enddate從函數存儲到數組中以供進一步處理。將函數的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'
[**無法複製**](http://codepad.viper-7.com/k1w1tL)爲每個月的 – Rikesh 2013-04-23 06:22:59
@Rikesh這個腳本打印開始和結束日期定期。但我無法將這些日期存儲在一個數組中。 – 2013-04-23 06:46:10
你能告訴我們你的願望輸出嗎? – Rikesh 2013-04-23 06:46:59