Please how can I manipulate Carbon date object to add the number of days in the current month to the date object itself.
I have the following html form:
<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>
Then 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'));
});
});
then in my controller:UserController class, I have :
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 ....
}
When I submit the form, the date is not working as expected. I want to add the number of days in the current month to the date object but it keeps returning today's date instead of current date + 31 days which should give me 2021-09-14.
Please what am I doing wrong?
You dont need to assign it in another variable. addDays()
modifies the current Carbon object.
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 ....
}