How to format date Yii?

1

I need an example function to change the date to the Brazilian standard.

    
asked by anonymous 24.03.2015 / 20:36

2 answers

3

You can use the following function:

Yii::$app->getFormatter()->asDate($variavel_para_formatacao)

Do not forget to make the necessary settings in the common / config / main.cfg

'components' => [
    //...
    'formatter' => [
        'dateFormat' => 'dd/MM/yyyy',
        'datetimeFormat' => 'dd/MM/yyy H:i',
        'timeFormat' => 'H:i',
        'decimalSeparator' => ',',
        'thousandSeparator' => '.',
        'currencyCode' => 'R$',
    ],
    //....
    
24.03.2015 / 20:40
-1

Try to use the native function to own PHP:

$mode->sua_data = date('d-m-Y', strtotime($model->sua_data));
  • date // Format a date for other formats

  • strtotime // Transforms a String into dateTime

Your date (probably removed from the database) will come as a normal string, so before using the date it is necessary to make it into dateTime. If a normal String is passed to the date it will transform it into a deault date (01-01-1970).

    
14.01.2017 / 06:36