我是Laravel的新手,我一直在搜索如何在下面的选择框中显示月份的名称
01-Januray
02-February
03-March
我怎么能实现这个目标呢?
<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>
控制器
public function create()
{
$getMonth = date('M', mktime(0,0,0,$m));
return view('create', compact('getMonth'));
}
}
答案 0 :(得分:0)
获取月份名称并将其转换为字符串,然后使用m
作为带有前导零的月份编号,使用date()
函数转换回日期格式。
{{ $month_num = date('m', strtotime($month)) }}
答案 1 :(得分:0)
&#39;&#39;为了接收数组中的月份:
"01-January"
"02-February"
"03-March"
"04-April"
"05-May"
"06-June"
"07-July"
"08-August"
"09-September"
"10-October"
"11-November"
"12-December"
您可以使用此示例控制器:
<?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;
}
}
}
答案 2 :(得分:0)
最简单的解决方法是更改此行
$getMonth = date('M', mktime(0,0,0,$m));
到
$getMonth = [];
foreach (range(1, 12) as $m) {
$getMonth[] = date('m - F', mktime(0, 0, 0, $m, 1));
}
答案 3 :(得分:0)
您可以使用此
{!! Form::selectMonth('month',null,['class' => 'form-control']) !!}