Reputation: 97
I have a edit page that shows a date and is possible to change the date and edit it.
I want to show the date in this format "25-08-18 - 15:30" if the date already is stored in DB. So I have this field:
<div class="form-group col-md-6">
<label for="date">Date</label>
<div class="input-group date" data-provide="datepicker">
<input type='text' onkeydown="event.preventDefault()"
name="date" value="{{!empty($post->date) ? $post->date->formatLocalized('j-m-y - H:i') : ''}}"
class="form-control" placeholder="DD/MM/YYY"/><span class="input-group-addon"><i class="fa fa-calendar text-primary"
aria-hidden="true"></i></span>
</div>
</div>
But like that it shows "j-m-y - H:i" in the input field value. Do you know why?
Like this:
value="{{!empty($post->date)
? $post->date->toDateTimeString()
: ''
}}
it shows the seconds "2018-08-25 15:30:00" but it should show only "2018-08-25 15:30".
Then to validate is used:
'date' => 'nullable|date_format:"j-m-y - H:i"',
And to update is used:
$post->date = (isset($request->date)) ?
Carbon::createFromFormat('j-m-y - H:i', $request->date) : null;
Upvotes: 0
Views: 114
Reputation: 35220
format
and formatLocalized
are not the same and are not interchangeable.
formatLocalized() uses strrftime() under the hood and is used for formatting local time/date according to locale settings and does not accept the same formatting string/characters as the format()
method.
You should be able to achieve what you're after by changing:
$post->date->formatLocalized('j-m-y - H:i')
to:
$post->date->format('j-m-y - H:i')
Upvotes: 1
Reputation: 1066
value="{{!empty($post->date)
? substr($post->date->toDateTimeString(),0,-2)
: ''
}}
Upvotes: 1