0
votes

Hello! I am working on Laravel project I have a form for invoices which include two date fields along with other fields. The two dates are named start_date and end_date. I want to select the end date based on start date which date should be the next month just one day before. For example .. I set the start date to 10 of March then the end date should be 9th of April. one day before the selected date of next month

My fields are..

<div class="col-lg-6">
   <div class="form-group">
       <label>Start Date</label>
             <input class="form-control" type="date" name="start_date" value="{{old('start_date') }}">
   </div>
</div>
<div class="col-lg-6">
    <div class="form-group">
        <label>End Date</label>
           <input class="form-control" type="date" name="end_date" value="{{ old('end_date') }}">
    </div>
</div>

How can this be done and should it be through the java script or can it be in controller. If we can do it in controller then I sure that we will not need the end date field and that will be no problem.. Thanks in advance!

1

1 Answers

0
votes

Simple Way to do it in controller is :

Carbon::parse($request->start_date)->addMonths(1)->subDays(1)

What it gonna do is, parse the date via carbon, add month to the start date, then delete a day from the added month.

You can just set it into variable. and store it in the database as end date, or just directly store it.