Get the last 12 months in PHP

后端 未结 5 1690
醉话见心
醉话见心 2020-12-14 16:46

So here is an interesting problem I learned today.

I need to populate an array with the last 12 months, starting with the past month. So in August 2011, the last 12

5条回答
  •  情深已故
    2020-12-14 17:10

    I'm sure someone has a more elegant solution, but you could start counting backwards from the 1st of this month.

    for ($i = 1; $i <= 12; $i++) {
        $months[] = date("Y-m%", strtotime( date( 'Y-m-01' )." -$i months"));
    }
    

提交回复
热议问题