Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change created_at format

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?

like image 247
Neon_10 Avatar asked Apr 12 '15 12:04

Neon_10


People also ask

How to change created_ at format in Laravel?

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.

How to change created_ at in Laravel?

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.

How do I change the default date format in Laravel?

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.


2 Answers

You can read more about strftime here

t.created_at.strftime("%Y-%m-%d")
like image 174
Mihail Petkov Avatar answered Oct 16 '22 14:10

Mihail Petkov


And it can be done even easier way:

t.created_at.to_date
like image 32
Tsvetkova Maria Avatar answered Oct 16 '22 14:10

Tsvetkova Maria