0

I am new to Laravel and I have been searching for how to display the names of the month in a select box like below

  01-Januray
  02-February
  03-March

How can I achieve this, please?

<div class="col-xl-4 col-lg-12 col-md-12">
    <fieldset class="form-group">
        <label for="placeTextarea">Expiration Month</label>
        <select  class="form-control" id="month" name="month" mulitple>
            <option> Select Month </option>
            @foreach($getMonth as $month)
                <option>
                    {!! $month!!}
                </option>
            @endforeach
        </select>                              
    </fieldset>
</div>

controller

public function create()
    {
        $getMonth = date('M', mktime(0,0,0,$m));
        return view('create', compact('getMonth'));
    }
}
Ignacio Ara
  • 2,476
  • 2
  • 26
  • 37

4 Answers4

0

Get the month name and transform it into a string and then convert back into a date format by using m as the month number with leading zeros using the date() function.

{{ $month_num = date('m', strtotime($month)) }}
0

''In order to receive the months in an array such as this:

"01-January"
"02-February"
"03-March"
"04-April"
"05-May"
"06-June"
"07-July"
"08-August"
"09-September"
"10-October"
"11-November"
"12-December"

You could use this example controller:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use DateTime;
use Generator;
use Illuminate\Http\Request;

class Example extends Controller
{
    public function create()
    {
        return view('create', ['months' => iterator_to_array($this->getMonths())]);
    }

    protected function getMonths(): Generator {
        foreach (range(1, Carbon::MONTHS_PER_YEAR) as $month) {

            $human = DateTime::createFromFormat('!m', $month)->format('F'); // https://stackoverflow.com/a/18467892/6056864

            yield str_pad($month, 2, 0, STR_PAD_LEFT) .'-'. $human;
        }
    }
}
Quezler
  • 2,376
  • 14
  • 29
0

the easiest fix is to change this line

$getMonth = date('M', mktime(0,0,0,$m));

to

$getMonth = [];
foreach (range(1, 12) as $m) {
    $getMonth[] = date('m - F', mktime(0, 0, 0, $m, 1));
}

PHP date function documentation

CuatroKB
  • 21
  • 4
0

You can use this

{!! Form::selectMonth('month',null,['class' => 'form-control']) !!}