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

0

Within a index.blade.php I have a variable:

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

The date comes out in English format. How do I put it in Brazilian format?

Note: version of Laravel 5.2

    
asked by anonymous 05.01.2017 / 23:59

3 answers

3

This should work:

   <td>{{ date( 'd/m/Y' , strtotime($ordemjoin->dataplanejamento))}}</td>
    
06.01.2017 / 00:13
1

DB does not have any all the features offered by Eloquent , then do the right 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 do not want to do so, you can work with strtotime with date , but, create a file of helpers and put a function to not be repeating code:

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

and Views ( blade ):

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

Recommendation:

If you start using Eloquent you will notice the programming gain in relation to using the DB class example of course I'll illustrate would be the Date Mutators in the class configuration, example :

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

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

in your Views file ( blade ) could do it like this:

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

It would be very easy and could format your date.

06.01.2017 / 00:16
0

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

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

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

{{$ordemjoin->dataplanejamento->format('d/m/Y')}}
    
06.01.2017 / 00:52