如何向 Carbon 日期对象添加动态天数
How can I add dynamic number of days to a Carbon date object
请问我如何操作 Carbon 日期对象以将当前月份的天数添加到日期对象本身。
我有以下 html 形式:
<form method="POST">
@csrf
<input type="hidden" name="plan" id="plan" />
<div class="form-group">
<h5>@lang('Enter Invest Amount')</h5>
<div class="input-group-append">
<input type="text" name="amount" class="form-control" />
<span class="input-group-text">{{ $general->cur_text }}</span>
</div>
<br>
<h5>@lang('Select Investment End Date')</h5>
<div class="input-group-append">
<input type="text" name="investmentEndDate" class="investmentEndDate form-control"/>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn--base" style="width:100%;">@lang('Invest')</button>
</div>
</form>
然后Javascript:
const date = new Date();
const daysInTheMonth = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
$(function () {
$('.investmentEndDate').datepicker({
minDate: daysInTheMonth
});
$('.planModal').on('click', function() {
$('#depoModal #plan').val($(this).data('plan'));
});
});
然后在我的 controller:UserController class 中,我有:
public function postplan(Request $request)
{
$today = Carbon::now();
$endDate = $today->addDays($today->daysInMonth);//emphasis here - it keeps returning today's date
$request->validate([
'plan' => 'required|numeric|exists:plans,id',
'amount' => 'required|numeric|min:1',
'investmentEndDate' => 'required|date|after_or_equal:'.$endDate
]);
var_dump($endDate);//returns today's date
//other code ....
}
当我提交表格时,日期没有按预期显示。我想将当月的天数添加到日期对象,但它一直返回今天的日期而不是当前日期 + 31 天,这应该给我 2021-09-14。
请问我做错了什么?
你不需要在另一个变量中分配它。 addDays()
修改当前 Carbon 对象。
public function postplan(Request $request)
{
$today = Carbon::now();
$today->addDays($today->daysInMonth);
$request->validate([
'plan' => 'required|numeric|exists:plans,id',
'amount' => 'required|numeric|min:1',
'investmentEndDate' => 'required|date|after_or_equal:'.$endDate
]);
var_dump($today);// this will be at the date of one month later
//other code ....
}
请问我如何操作 Carbon 日期对象以将当前月份的天数添加到日期对象本身。
我有以下 html 形式:
<form method="POST">
@csrf
<input type="hidden" name="plan" id="plan" />
<div class="form-group">
<h5>@lang('Enter Invest Amount')</h5>
<div class="input-group-append">
<input type="text" name="amount" class="form-control" />
<span class="input-group-text">{{ $general->cur_text }}</span>
</div>
<br>
<h5>@lang('Select Investment End Date')</h5>
<div class="input-group-append">
<input type="text" name="investmentEndDate" class="investmentEndDate form-control"/>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn--base" style="width:100%;">@lang('Invest')</button>
</div>
</form>
然后Javascript:
const date = new Date();
const daysInTheMonth = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
$(function () {
$('.investmentEndDate').datepicker({
minDate: daysInTheMonth
});
$('.planModal').on('click', function() {
$('#depoModal #plan').val($(this).data('plan'));
});
});
然后在我的 controller:UserController class 中,我有:
public function postplan(Request $request)
{
$today = Carbon::now();
$endDate = $today->addDays($today->daysInMonth);//emphasis here - it keeps returning today's date
$request->validate([
'plan' => 'required|numeric|exists:plans,id',
'amount' => 'required|numeric|min:1',
'investmentEndDate' => 'required|date|after_or_equal:'.$endDate
]);
var_dump($endDate);//returns today's date
//other code ....
}
当我提交表格时,日期没有按预期显示。我想将当月的天数添加到日期对象,但它一直返回今天的日期而不是当前日期 + 31 天,这应该给我 2021-09-14。
请问我做错了什么?
你不需要在另一个变量中分配它。 addDays()
修改当前 Carbon 对象。
public function postplan(Request $request)
{
$today = Carbon::now();
$today->addDays($today->daysInMonth);
$request->validate([
'plan' => 'required|numeric|exists:plans,id',
'amount' => 'required|numeric|min:1',
'investmentEndDate' => 'required|date|after_or_equal:'.$endDate
]);
var_dump($today);// this will be at the date of one month later
//other code ....
}