使用php中的一个函数回显前一年和前一个月


echo the before year and month using a function in php?

我是php的新手,假设我的博客创建于2011年1个月。文章将时间存储为这个1305357473。现在我想使用一个函数,它可以像这样输出前一年和蛾。

2011 year  1 month
2011 year  2 month
2011 year  3 month
2011 year  4 month
2011 year  5 month
......

我想使函数可以输出当前和过去的一年和一个月。这是我的函数并输出结果。但我不知道该怎么结束。

function outYearMonth($year,$month){
    $year=date(Y);
    $past_month=strtotime(date(1)) ; 
    $month=strtotime(date(m)); 
    $jg_month=round($month - $past_month);
    $month=array();
    for($i==0;$i<=$jg_month;$i++){
        $month[]= date(1)+1;
    }
}

此代码:

$months = 10; //count of months 
$date = date_create( 'now' );
echo date_format( $date, 'Y M' );
for ( $i = 0; $i < $months; $i++ ) {
    date_sub( $date , date_interval_create_from_date_string( '1 months' ) );
    echo date_format( $date, 'Y M' );
}

将输出:

2011 May
2011 Apr
2011 Mar
2011 Feb
2011 Jan
2010 Dec
2010 Nov
2010 Oct
2010 Sep
2010 Aug
2010 Jul
echo date('Y m');

会给你今年和月份的

echo date('Y m',strtotime('last month',time())) 

会给你上个月的

对于整个月的循环,只需获取当前月份并进行循环

$currentMonth = (int) date('m',time());
$currentYear = date('Y',time());
for($i=0; $i <= $currentMonth; $i++) {
    echo "$currentYear $i";
}

将反映所有月份的

2011 1
2011 2
2011 3
2011 4
2011 5