How to convert a date variable to the Brazilian format within a view?

Asked

Viewed 8,807 times

3

Within a index.blade.php have a variable:

<td>{{$ordemjoin->dataplanejamento}}</td>

In the listing comes the date in the format English. How do I put in the format Brazilian?

Observing: version of Laravel 5.2

  • the return of $ordemjoin->dataplanejamento is datetime?

  • you use the so DB::select('select * from users where id = :id', ['id' => 1]); to bring data???

  • Yes I use DB. I still don’t know how eloquent works properly so I do the joins

  • @Marcosbirro I made an answer by chance helped, or not?

  • It did. I’ll use the helpers.

  • @Marcirro a question has some problem in the given answers that you do not accept as answers? I saw on your page that never gave

  • I accepted them all. I don’t see any that I didn’t answer They’re all helping me..

  • Accept is to put blue check at the bottom of the arrow and score with a vote if you want. On your page are without.

  • Sure. It would be great.

  • Truth @Marcosbirro if you can and want to read these link 1, link 2 and link 3

Show 5 more comments

4 answers

4


That should work:

   <td>{{ date( 'd/m/Y' , strtotime($ordemjoin->dataplanejamento))}}</td>

2

With class only use DB does not have all the features offered by Eloquent, so do it right on SQL: DATE_FORMAT(dataplanejamento,'%d/%m/%Y') as dataplanejamentobr

DB::select("SELECT tabela.*, 
         DATE_FORMAT(dataplanejamento,'%d/%m/%Y') as dataplanejamentobr from tabela");

<td>{{$ordemjoin->dataplanejamentobr}}</td>

If you still don’t want to do so you can work with strtotime with date, but creates a helpers and put a function to not keep repeating code:

function date_br($value, $format='d/m/Y')
{
   return date($format, strtotime($value));
}

and in the Views (blade):

<td>{{date_br($carros->dataplanejamento)}}</td>

Recommending:

If you start using the Eloquent will realize the gain of programming in relation to using the class DB a clear example that I will illustrate would be the Date Mutators in class configuration, example:

In the array of $dates enter the field date or datetime in your case dataplanejamento:

class Carros extends Model
{    
    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
        'dataplanejamento'
    ];
}

in his file of Views (blade) could do straight like this:

<td>{{$carros->dataplanejamento->format('d/m/Y')}}</td>

it would be very easy and could format your date.

References:

0

Voce can use Casts passing in your model it returns the date in the correct format

protected $casts = [
    'dataplanejamento' => 'datetime:d/m/Y',
];

0

Apparently by what you describe in the question and comments, the object must be a string. In this case the example below should solve:

{{Carbon\Carbon::parse($ordemjoin->dataplanejamento)->format('d/m/Y')}}

If the object is a Carbon, just invoke the method format() directly from the object itself.

{{$ordemjoin->dataplanejamento->format('d/m/Y')}}
  • Replace the line with this: {{Carbon Carbon::parse($ordemjoin->dataplanning)->format(’d/m/Y')}} Result: Brought nothing. Maybe Carbon is not installed. You have to know?

  • I’m using Vagrant ok?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.