Reputation: 495
I need your help with something. I cant find to work this one out. I think it should be really simple, but it has kept me busy for hours.
{{Form::date('age', $data[0]->age)}}
When I use the form::date function I let a user select a birthday. After selecting the information is saved to a database in the following format ("Y-m-d"), this works perfectly.
If the user wants to edit the date of birth, it shows it as ("d-m-Y"), how can I show this as ("Y-m-d")? I have been searching online for hours, have tried it with carbon, with date_format and a lot more, nothing seems to work.
What am I doing wrong here? I am using laravel 5.2
Thank you for the help.
UPDATE
The query to get the information from the Mysql panel:
$userId = auth()->user()->id;
$data = Setting::where('user_id', '=', $userId)->first();
return view('pages_user.mysettings')->with('data', $data);
The model:
protected $dates = ['age'];
protected $table = 'date_userinfo';
The input box
{{ Form::date('age', $data->age->format('Y-m-d')) }}
Upvotes: 2
Views: 12277
Reputation: 495
The problem is google chrome, not laravel. Google chromes changes the date to the local standards.
Upvotes: 0
Reputation: 2261
Try this:
{{ Form::date('age', date('Y-m-d', strtotime($data[0]->age))) }}
It will convert date into Y-m-d
format.
I would strongly recommend checking out Carbon. It's built in with Laravel and works well with dates.
Update: In your Model set:
protected $dates = ['age'];
Then, in your view :
{{ Form::date('age', $data[0]->age->format('Y-m-d')) }}
Also, I wouldn't recommend using $data[0]
to get the data. If it's multiple rows, use a foreach
, otherwise you can pull one record (instead of multiple) by using ->first()
in your query.
Edit:
Workaround but forces it to work as long as the date format is d-m-Y
{{ Form::date('age', \Carbon\Carbon::createFromFormat('d-m-Y', $data->age)->format('Y-m-d') }}
Upvotes: 2