2017-09-05 56 views
0

生成一個月&年從兩個日期數組中的PHP生成一個月及一年陣列從兩個日期在PHP

我有 MM/YYYY形式
例如:

$start = "2/2016"; 
$end = "11/2017" 

我需要輸出所有陣列月和年 像這樣在PHP

$monthArray = Array ( 
       [0] => 2 
       [1] => 3 
       [2] => 4 
       [3] => 5 
       [4] => 6 
       [5] => 7 
       [6] => 8 
       [7] => 9 
       [8] => 10 
       [9] => 11 
       [10] => 12 
       [11] => 1 
       [12] => 2 
       [13] => 3 
       [14] => 4 
       [15] => 5 
       [16] => 6 
       [17] => 7 
       [18] => 8 
       [19] => 9 
       [20] => 10 
       [21] => 11 
      ); 

$yearArray = Array ( 
       [0] => 2016 
       [1] => 2016 
       [2] => 2016 
       [3] => 2016 
       [4] => 2016 
       [5] => 2016 
       [6] => 2016 
       [7] => 2016 
       [8] => 2016 
       [9] => 2016 
       [10] => 2016 
       [11] => 2017 
       [12] => 2017 
       [13] => 2017 
       [14] => 2017 
       [15] => 2017 
       [16] => 2017 
       [17] => 2017 
       [18] => 2017 
       [19] => 2017 
       [20] => 2017 
       [21] => 2017 
      ); 

功能會更好。請幫忙。在此先感謝

+7

你試過到目前爲止什麼碼?有什麼問題?錯誤? –

+4

這種期望的格式並不容易處理。爲什麼不只是一個'DateTime'對象的數組? – iainn

+0

我已經實現了。但沒有功能。長達3年只有 –

回答

6

如果你想使用的日期時間,你可以這樣做:

<?php 
$start = date_create_from_format("m/Y","2/2016")->modify("first day of this month"); 
$end = date_create_from_format("m/Y","11/2017")->modify("first day of this month"); 

$timespan = date_interval_create_from_date_string("1 month"); 

$months = []; 
$years = []; 

while ($start <= $end) { 
    $months[] = $start->format("m"); 
    $years[] = $start->format("Y"); 
    $start = $start->add($timespan); 

} 

print_r([ $months, $years ]); 

例子:http://sandbox.onlinephpfunctions.com/code/8bfa8cc9481aa8a13d83f62d9c1c6c7927654842

+0

工作非常感謝 –

1
<?php 
$start = "2/2016"; 
$end = "11/2017"; 

$start = explode('/', $start); 
$end = explode('/', $end); 

$d1 = strtotime($start[1] . '-' . $start[0] . '-01'); 
$d2 = strtotime($end[1] . '-' . $end[0] . '-01'); 
$min_date = min($d1, $d2); 
$max_date = max($d1, $d2); 

$month = []; 
$year = []; 
while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) { 
    $month[] = date('m',$min_date); 
    $year[] = date('Y',$min_date); 
} 
print_r($month); 
print_r($year); 
1

這應該工作速度更快,使用較少的內存比現有的答案,因爲它不」 t依賴於日期對象的創建和操作(特別是將字符串解析爲日期對象):

/** 
* Takes start and end date string in format 'mm/yyyy' along with a $months and $years 
* arrays; modifies the arrays in place to add all months and years between the dates 
*/ 
function listMonthsAndYears($start, $end, &$months, &$years) { 

    list($startM, $startY) = array_map('intval', explode('/',$start)); 
    list($endM, $endY)  = array_map('intval', explode('/',$end)); 
    $m = $startM; 
    $y = $startY; 

    while($endY > $y || ($endY === $y && $endM >= $m)){ 
    $months[]= $m; 
    $years[] = $y; 
    $m++; 
    if($m > 12){ // loop to the next year 
     $m = 1; 
     $y++; 
    } 
    } 
} 

用法:

$start = '2/2016'; 
$end = '11/2017'; 
listMonthsAndYears($start, $end, $months, $years); 

Live demo

相關問題