DEV Community

Cover image for How to get all months from the year  using in Laravel
Vickyvn
Vickyvn

Posted on • Updated on

How to get all months from the year using in Laravel

$month = [];

for ($m=1; $m<=12; $m++) {
     $month[] = date('F', mktime(0,0,0,$m, 1, date('Y')));
}

print_r($month);
Enter fullscreen mode Exit fullscreen mode

date('F') will get result current month of the year. example October.
so with the help of mktime(0,0,0,2, 1, date('Y')) we will get output February.
so we replace 2 to $m in for each and get answer in $month array.
we will get output like this.

Array ( 
 [0] => January
 [1] => February 
 [2] => March
 [3] => April
 [4] => May
 [5] => June
 [6] => July
 [7] => August
 [8] => September
 [9] => October
 [10] => November
 [11] => December 
)
Enter fullscreen mode Exit fullscreen mode

Discussion (0)