Goal:
- have two columns (datetime) save an input field that only takes in date
I would like the start date to save 11/11/2021 00:00:00
and the end date 11/12/2021 23:59:59
(only the date can be changed) from a text field that a user can only place a date in the text field:
<div class="row" id="custom_date"> <div class="col-md-4"> <p class="font-weight-bold">From:</p> </div> <div class="col-md-8"> <input type="date" id="custom_start_datetime" name="custom_start_datetime"> </div> <div class="col-md-4"> <p class="font-weight-bold">To:</p> </div> <div class="col-md-8"> <input type="date" id="custom_end_datetime" name="custom_end_datetime"> </div> </div>
snippet of my store function:
$shift->effective_start_datetime = $request_add->input('effective_start_datetime'); $shift->effective_end_datetime = $request_add->input('effective_end_datetime');
the code above gives an error because the row is in timestamp without time zone
and it gets the data from the text fields that only takes in the date and no time.
What can I change in my function code to have the date save but in the h/m/s format it could have 00:00:00
for the start_datetime
and 23:59:59
end_datetime
automatically?
for example, if I input in the two text fields 08/11/2021
and 08/13/2021
it will be saved as: 08/11/2021 00:00:00
and 08/13/2021 23:59:59
Advertisement
Answer
You can use Carbon class to manipulate your dates, so, if you want to put the time, you can use parse
method and use startOfDay
and endOfDay
methods.
Example:
$startDate = Carbon::parse($firstDate)->startOfDay(); // Will return a Carbon Instance at 00:00:00 $endDate = Carbon::parse($secondDate)->endOfDay(); // Will return a Carbon Instance at 23:59:59
These methods will solve your problem, and if you need a time zone, you can use toISOString
method after startOfDay
and endOfDay
, that is the default format of API response.
$endDate = Carbon::parse($secondDate)->endOfDay()->toISOString(); // 2021-08-12T23:21:54.788279Z