1

Im have a simple task as i think but im have some troble with this

Mb anyone know another way how to get all months of the year with Carbon using

In this sutiation im need only short name of month

At this time im have the next code

$items = [];
$startMonth = Carbon::now()->startOfYear()->format('M');
$endMonth = Carbon::now()->endOfYear()->format('M');
$monthRange = CarbonPeriod::create($startMonth, '1 month', $endMonth);
foreach ($monthRange as $month){
   $items[] = Carbon::parse($month)->format('M');
}

Are there any solutions without overwriting the variable $items

Thanks for help

2 Answers2

5
$month = [];

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

print_r($month);
iAmGroot
  • 950
  • 1
  • 6
  • 22
0

One line solution:

array_map(fn($month) => Carbon::create(null, $month)->format('F'), range(1, 12))

range(1, 12) returns an array [1, .., 12]. Then, we're transforming each of the element to full month name using Carbon.