有沒有在PHP的方式獲得當前和以前的5個月在以下格式?PHP:得到最後6個月的格式月份年
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
有沒有在PHP的方式獲得當前和以前的5個月在以下格式?PHP:得到最後6個月的格式月份年
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
你試過以下:
<?php
echo date('F, Y');
for ($i = 1; $i < 6; $i++) {
echo date(', F Y', strtotime("-$i month"));
}
讓我知道,如果這不會工作。
試試這個
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month"));
}
你錯過了當前日期。 $ j應該在0到今天。 – azngunit81
謝謝 - 這也非常有幫助。 – user2571510
爲什麼不使用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);
<?php
for ($i =0; $i < 6; $i++) {
$months[] = date("F Y", strtotime(date('Y-m-01')." -$i months"));
}
print_r($months)
?>
試試這個..
謝謝 - 這也非常有幫助。 – user2571510
非常感謝 - 我會用這個去。 :) – user2571510
不客氣! – Tyralcori