This should be a simple thing. Well, Im sure it should be simple, this is rails.
The problem is: in the model all data has a field created_at. to retrieve this info in the view I use a block, where is a line t.created_at.
It shows me a result like 2015-04-12 11:04:44 UTC
Which method should I use to show this date as 2015-04-12? As I suppose it should be something like that: t.created_at.date_only
Could you help me?
You can change the date format in Laravel blade view using the format() method on created_at attribute by passing the date format in which you want to display the date in your view file. You don't have to make any changes to your model or controller file. Just call the format method in the view file.
Any model just use this code in your model class. You can change it whatever you want like how to get only date from created at in laravel. Now you can use it $entry->created_at_formatted when it's needed.
So, let's add these variables to config/app. return [ 'date_format' => 'm/d/Y', 'date_format_javascript' => 'MM/DD/YYYY', ]; And then, in Laravel, we can use this: Carbon::createFromFormat(config('app. date_format'), $value)->format('Y-m-d'); Carbon::parse($value)->format(config('app.
You can read more about strftime
here
t.created_at.strftime("%Y-%m-%d")
And it can be done even easier way:
t.created_at.to_date
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With