Timestamp or date, you must use date for a date field, not a timestamp. Look up yourself unix timestamp.
Date reverts to 1970-01-01 00:00:00 instead of correct date value
I have a form set up to edit a selected date range if the user needs to. When I check the value of the dates they show that the correct old date is being pulled from the database. However, if I only change one of the dates in the form and hit save, the date that is not changed reverts to 1970-01-01 00:00:00. From what I have read this is because the value is null. So why is my date showing up but not being transferred into the database again?
Here is my controller update function:
public function update(Request $request, $id)
{
$this->validate($request, [
'date' => 'min:3 | nullable',
'second_date' => 'min:3 | nullable',
'holiday_name' => 'min:3 | nullable',
'hours' => 'min:3 | nullable',
]);
$holiday = Holiday::find($id);
// dd($holiday);
$holiday->date = date('Y-m-d ', strtotime($request['date']));
$holiday->second_date = date('Y-m-d', strtotime($request['second_date']));
$holiday->holiday_name = $request->input('holiday_name');
$holiday->update();
return redirect()->route('holiday.index');
}
And my form in the view is this:
<form class="form--admin" action="{{ route('holiday.update', $holiday->id)}}" method="post" enctype="multipart/form-data">
@method('PATCH')
@csrf
<fieldset>
<div class="formrow">
<div class="formitem">
<label for="date" class="label">Date: {{ $holiday->date->format(' F jS ') }}</label>
<input type="date" value="{{ old('date', $holiday->date) }} " name="date" id="date"></input>
</div>
</div>
<div class="formrow">
<div class="formitem">
@if(empty($holiday->second_date))
<label for="second_date" class="label">Select a second Date </label>
@else
<label for="second_date" class="label">Date: {{ $holiday->second_date->format('F jS ') }}</label>
@endif
<input type="date" value="{{ $holiday->second_date }} " name="second_date" id="second_date"></input>
</div>
</div>
<div class="formrow">
<div class="formitem">
<label for="holiday_name" class="label">Holiday Name</label>
<input type="text" value="{{ $holiday->holiday_name }} " name="holiday_name" id="holiday_name"></input>
</div>
</div>
<a href="{{ route('holiday.index', ) }}" class="button btn--cancel" role="button">Cancel</a>
<button class="primary button" type="submit">Save Changes</button>
<a href="{{ route('showroom')}} " class="button btn-edit" target="_blank">View</a>
</fieldset>
</form>
In the database the date should be:
YYYY-MM-DD
That's how MySql stores dates. What format do you have,
Usually
$tdate = new \DateTime($somedate); got from input
$ndate = $tdate->format('Y-m-d');
$ndate gets stored. If you have a database where the date format is not correct, fix that.
Please or to participate in this conversation.