英文:
How to get last date of month by passing a date which varies on situation in Laravel?
问题
我已从月份字段中选择了一个月。
<input class="form-control" type="month" id="report_month" name="report_month"
value="{{ old('report_month') }}">
该值为'2023-02'。
如何在控制器中获取该特定月份的最后日期?
所需值为:2023-02-28。
英文:
I have selected a month from month field
<input class="form-control" type="month" id="report_month" name="report_month"
value="{{ old('report_month') }}">
That value is '2023-02'.
How to get last date of that specific month in controller?
Value required is :2023-02-28
答案1
得分: 1
Sure, here are the translated parts:
@php
$lastDayofMonth1 = Carbon\Carbon::createFromFormat('Y-m', $request->input('report_month'))->endOfMonth()->day;
@endphp
@php $lastDayofMonth = \Carbon\Carbon::now()->modify('-2 months')->endOfMonth()->toDateString(); @endphp
@php
$month = 5;
$lastDayofMonth = \Carbon\Carbon::today()->setMonth($month)->endOfMonth()->day;
@endphp
英文:
@php
$lastDayofMonth1 = Carbon\Carbon::createFromFormat('Y-m', $request->input('report_month'))->endOfMonth()->day;
@endphp
another ways
@php $lastDayofMonth = \Carbon\Carbon::now()->modify('-2 month')->endOfMonth()->toDateString(); @endphp
used ->modify('-2 month')
part is dynamically, we get values
OR
@php
$month = 5;
$lastDayofMonth =\Carbon\Carbon::today()->setMonth($month)->endOfMonth()->day;
@endphp
答案2
得分: 0
$input = $request->input('report_month'); // 2023-02
[$year, $month] = explode('-', $input); // [2023, 02]
today()->setYear($year)->setMonth($month)->endOfMonth()->format('Y-m-d');
or
Carbon::createFromFormat('Y-m', $request->validated('report_month'))
->endOfMonth()->format('Y-m-d');
英文:
$input = $request->input('report_month'); // 2023-02
[$year, $month] = explode('-', $input); // [2023, 02]
today()->setYear($year)->setMonth($month)->endOfMonth()->format('Y-m-d');
or
Carbon::createFromFormat('Y-m', $request->validated('report_month'))
->endOfMonth()->format('Y-m-d');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论