2011-03-10 35 views
1

我正在使用在php中選擇最後5個日期

$todayDate = date('Y-m-d'); 

來獲取今天的日期。

我不知道如何從現在的日期開始選擇最後5個日期。有沒有選項通知我如何從現在的日期選擇最後5個日期。 在此先感謝

回答

6
$dates = array(); 
for ($i = 0; $i < 5; $i++) { 
    $dates[] = date('Y-m-d', strtotime("-$i days")); 
} 

print_r($dates); 
+0

打我吧... :)必須承認,你的更優雅,缺乏文檔鏈接,雖然 –

+0

嗯,他確實似乎是要求一個日期範圍。我在心裏過濾掉了。 – Charles

+0

@Charles同上。 – Kyle

2

strtotime可以做到這一點。

$five_days_ago = strtotime('-5 days'); 
$five_days_formatted = date('Y-m-d', $five_days_ago); 
0
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y")); 
0

您可以使用功能日期的strtotime :)

$fiveDaysFuture = date('Y-m-d', strtotime('+5 days', strtotime(date('Y-m-d'))));//future 
$fiveDaysAgo = date('Y-m-d', strtotime('-5 days', strtotime(date('Y-m-d'))));//ago 
相關問題