开发者

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

i am a newbie of php, supposed my blog created on 2011 year 1 month.and the article stored time as this 1305357473. now i want to use a function, which can output the before year and moth as this.

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

i want to make the function can ou开发者_如何学运维tput the current and the past year and month. this is my function and output the result. but i don't know how to finish.

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;
    }
}


This code:

$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' );
}

Will output:

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');

will give you this year and month

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

will give you last month

for looping through all the month just get the current month and make a loop

$currentMonth = (int) date('m',time());
$currentYear = date('Y',time());

for($i=0; $i <= $currentMonth; $i++) {
    echo "$currentYear $i";
}

will echo all months

2011 1

2011 2

2011 3

2011 4

2011 5
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜