2012-11-20 33 views
2

可能重複:
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?PHP - 列出一個月的所有天一年

我是新來的PHP。我如何能夠列出一年中的所有日子? (或從給定的日期?)

我想要做的是生成一個月的所有日子的形式,所以我可以填寫預算。它應該在今年年底結束,但它應該能夠從一個隨機的日期開始。

我應該怎麼做的任何例子?我也應該能夠看到日期的名稱。例如「5月21日 - 週一」,並列出所有的日,月,直到2013年

+0

嘗試檢查出'的strtotime()' 。 [繼承人一個很好的教程](http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function/) 。 – cpdt

+0

更好的教程:http://php.net/manual/en/function.strtotime.php :-) – eisberg

+0

@eisberg IMO另一個教程更適合OP想要什麼。 – cpdt

回答

4

您可以使用此功能,但有可能是一個更優雅的方式:

$start_date = '2012-06-01'; // Give in your own start date 
$start_day = date('z', strtotime($start_date)); // 6th of June 
$days_in_a_year = date('z', strtotime('2012-12-31')); // 31th of december 

$number_of_days = ($days_in_a_year - $start_day) +1 ; // Add the last of december also 
for ($i = 0; $i < $number_of_days; $i++) { 
    $date = strtotime(date("Y-m-d", strtotime($start_date)) . " +$i day"); 
    echo date('d F - l', $date) .'<br />'; 
} 
相關問題