2014-04-01 131 views

回答

8

你試過以下:

<?php 
echo date('F, Y'); 
for ($i = 1; $i < 6; $i++) { 
    echo date(', F Y', strtotime("-$i month")); 
} 

讓我知道,如果這不會工作。

+0

非常感謝 - 我會用這個去。 :) – user2571510

+0

不客氣! – Tyralcori

2

使用strtotimedate

for($i = 0; $i <= 5 ; $i++) { 
    print date("F Y", strtotime("-".$i." month"))."\n"; 
} 

實現另一個格式的日期看PHP的日期格式HERE

3

試試這個

for ($j = 0; $j <= 5; $j++) { 
    echo date("F Y", strtotime(" -$j month")); 
} 
+0

你錯過了當前日期。 $ j應該在0到今天。 – azngunit81

+0

謝謝 - 這也非常有幫助。 – user2571510

1

爲什麼不使用DateTime對象爲

$start = new DateTime('first day of this month - 6 months'); 
$end = new DateTime('last month'); 
$interval = new DateInterval('P1M'); // http://www.php.net/manual/en/class.dateinterval.php 

$date_period = new DatePeriod($start, $interval, $end); 
$months = array(); 
foreach($date_period as $dates) { 
    array_push($months, $dates->format('F').' '.$dates->format('Y')); 
} 

print_r($months); 
0
<?php 
for ($i =0; $i < 6; $i++) { 
    $months[] = date("F Y", strtotime(date('Y-m-01')." -$i months")); 
} 

print_r($months) 
?> 

試試這個..

+0

謝謝 - 這也非常有幫助。 – user2571510

相關問題