2012-11-12 72 views

回答

16

嘗試:

// for each day in the month 
for($i = 1; $i <= date('t'); $i++) 
{ 
    // add the date to the dates array 
    $dates[] = date('Y') . "-" . date('m') . "-" . str_pad($i, 2, '0', STR_PAD_LEFT); 
} 

// show the dates array 
var_dump($dates); 
+0

直截了當,很好。日期+1('t')。 – Jean

2

如何:

$list=array(); 
for($d=1; $d<=31; $d++) 
{ 
    $time=mktime(12, 0, 0, date('m'), $d, date('Y')); 
    if (date('m', $time)==date('m')) 
     $list[]=date('Y-m-d', $time); 
} 
var_dump($list); 
+0

如果月份少於31天? – Moozy

+1

然後他們被if跳過。 –

+0

感謝這個快速答案,正是我需要的! – Moozy

4

一個簡單的函數返回這樣的陣列可以是這樣的:

function range_date($first, $last) { 
    $arr = array(); 
    $now = strtotime($first); 
    $last = strtotime($last); 

    while($now <= $last) { 
    $arr[] = date('Y-m-d', $now); 
    $now = strtotime('+1 day', $now); 
    } 

    return $arr; 
} 

如果需要的話,可以提高它通過將步驟(+1 day)和輸出格式(Y-m-d)更改爲可選參數ERS。

+1

最適合我的情況,當我有一個開始和結束的日期,謝謝 – JochemQuery

相關問題