2014-04-01 81 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

相关问题