2

I send from a controller this query:

$tareas = $this->Tests->Tareas->find('list', [
            'limit' => 200, 
            'keyField' => 'id', 
            'valueField' => 'Fecha'
        ]);

The valueField 'Fecha' is a date in my database.

So far so good, but in the view this date field show in 'M-d-Y' format and i want 'dd-MM-yyyy' format.

The problem is that i use input for show a list of dates and i don't know to change the format.

My view have:

echo $this->Form->input('tarea_id', [
                'options' => $tareas,
            ]);

How i can to change this date format in $tareas? I try with:

Cake\I18n\I18n::locale('es-ES');

But this only sawpping order of day by month.

Thank you very much in advance.

3 Answers3

1

After much reading and practice, I have found a partial solution. I wanted to change the date format only in that object. But this solution is good for the moment.

I change to configuration of file config/bootstrap.php

In the position of this file:

ini_set('intl.default_locale', Configure::read('App.defaultLocale'));

Changed to:

ini_set('intl.default_locale', 'es_ES');
Cake\I18n\Date::setToStringFormat('dd-MM-YYYY');
Cake\I18n\FrozenDate::setToStringFormat('dd-MM-YYYY');
Cake\Database\Type::build('date')
    ->useImmutable()
    ->useLocaleParser()
    ->setLocaleFormat('dd-MM-YYYY');

This gets reformat all site dates with the format indicated.

If there is another solution would be appreciated,

Thank all for reading me.

1

I assume your using Cakephp 3.0

I found a better solution, is go to config/app.php file.

look for;

'App' => [
        'namespace' => 'App',
        'encoding' => env('APP_ENCODING', 'UTF-8'),
        'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
        'base' => false,

edit the line to suit your local;

for ireland its;

'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_IE'),

spain;

'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_ES'),
fuzzy dunlop
  • 477
  • 2
  • 10
  • 22
1

You can create entity in model/entity/ for example your model name is users so you can create bellow entity

User.php

namespace App\Model\Entity;

use Cake\ORM\Entity;

class User extends Entity {

       protected function _getList() {
          return date("Y-m-d",strtotime($this->_properties['Fecha']));
       }

}

Now You can use

$tareas = $this->Tests->Tareas->find('list', [
            'limit' => 200, 
            'keyField' => 'id', 
            'valueField' => function ($e) {
                        return $e->get('list');
                    }
        ]);

For more information http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs