2

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')) }} 
2
  • What is the output of $data[0]->age? Commented Jul 11, 2018 at 1:53
  • A date of birth in the output as ("Y-m-d"), for example 1988-12-31 Commented Jul 11, 2018 at 1:54

2 Answers 2

2

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') }}

Sign up to request clarification or add additional context in comments.

17 Comments

I tried this one, didn't changed the output. It still shows as ("d-m-Y")
Where is your $data coming from? Are you pulling from a model?
I am pulling it from the controller, straight from the MySQL database, the row is called age with a datatype of date. If I would only echo the $data[0]->age, it shows a correct date as ("Y-m-d").
Ok. Do you have a model configured? Say, I have a table called users, I should have a User model setup and I would pull data via $users = Users::get(). Once you have this you can tell your model which column contains dates and it will automatically format it for you.
I found the solution to the problem. Laravel wasn't the problem, google chrome is! As I use English, it automatically converts it, when I, for example, would change to Japanese, it shows it the right way. Google chrome; changes it.
|
0

The problem is google chrome, not laravel. Google chromes changes the date to the local standards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.