I've recently implemented a bootstrap-datetimepicker.js which is based on moments.js for some visual effects. Before the datetime widget I simply had an input field with date&time. Now I am facing some issues, there is a kind of conflict between Carbon and Monents.js with the datetime format.
This is my inputfield:
<div class="form-group">
<div class="col-sm-4">
{!! Form::label('published_at', 'Veröffentlichungsdatum') !!}
</div>
<div class="col-sm-8">
{!! Form::input('datetime','published_at', null, ['class' => 'form-control datetimepicker']) !!}
</div>
</div>
When I save an artcile this exception is thrown:
"InvalidArgumentException in Carbon.php line 425:
Unexpected data found.
Unexpected data found.
Unexpected data found.
Trailing data"
Without using the BS datetimepicker my datetime is looking like this:
2016-10-07 16:32
I am using this in My Model to format it properly:
public function setPublishedAtAttribute($date) {
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d H:i', $date);
}
However, with the BS datetimepicker my datetime is looking like this:
10/25/2016 21:35
I have no idea how to fix this issue.
I've always done it like this, hope it helps:
/**
* Define how the date attribute gets saved to the database
* @param $date
*/
public function setPublishedAtAttribute($date)
{
$this->attributes['start_date'] = Carbon::createFromFormat('Y-m-d H:i', $date)->toDateTimeString();
}
Note the ->toDateTimeString()