0

CakePHP 3.5.13. I am getting some records from MySQL which are stored as DATE fields. For example: 2018-07-30

I want Cake to output the dates in that format. However, it is converting them to British dd/mm/yyyy format. So the example above comes out as 30/07/2018 in my template files.

The data returned from the database looks like this:

'date' => object(Cake\I18n\FrozenDate) {

        'time' => '2018-07-30T00:00:00+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },

In my config/app.php the locale is set to en_GB:

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

I assume this is the reason it's displaying all of the dates in this way.

All I am doing in the template is displaying the data returned from the database - no conversion is occurring within my code:

// In a controller
$RevisionSubstances = TableRegistry::get('RevisionSubstances');
$revision_comments = $RevisionSubstances->find('all')->toArray();
debug($revision_comments); // format is '2018-07-30T00:00:00+00:00'

// In the template
echo $revision_comments['date']; // format is '30/07/2018'

How do I get the dates to be output in yyyy-mm-dd format?

I had a look at these resources but can't see the answer:

Andy
  • 5,142
  • 11
  • 58
  • 131
  • 1
    See for example **https://stackoverflow.com/questions/35676227/cakephp-3-2-change-default-date-format** if you want to change the defaults globally. – ndm Oct 11 '18 at 08:47
  • @ndm I've looked at that and it works ok for Dates. What if there is a Time involved as well? Some parts of my application use MySQL `DATE` and others have `DATETIME` data, which may also include a time. I still need to output the time with the date portion in the same format, e.g. `2018-07-30 11:15` – Andy Oct 11 '18 at 14:37
  • You'd have to additionally configure `\Cake\I18n\Time` and `\Cake\I18n\FrozenTime` accordingly. – ndm Oct 11 '18 at 16:38

0 Answers0