8

I am using cakephp 3.2 and when i am retrieving data by find query it is giving date fields in this format

Array
(
[0] => Cake\I18n\FrozenDate Object
    (
        [date] => 2016-08-01 00:00:00
        [timezone_type] => 3
        [timezone] => UTC
    )
)

and time fields in frozentime

Cake\I18n\FrozenTime Object
(
 [date] => 2016-10-11 10:00:00
 [timezone_type] => 3
 [timezone] => UTC
)

I need a common setting or global solution for complete site. So when i fetch the data by find query from database it should give me date time in simple format without any frozendate object.

like this

Array(
 [0] => 2016-08-01
)
aman
  • 125
  • 1
  • 11
  • None of the answers to date seem to meet the OP's request that "I need a **common setting or global solution** ... so when i fetch the data ... it should give me date time in simple format without any frozendate object." Current answers require code changes everywhere the date is used. – MM. Apr 19 '22 at 18:31

4 Answers4

8

Simply call ->format('Y-m-d') on your Cake\I18n\FrozenDate object.

There's no need for Cake\I18n\FrozenDate::setToStringFormat() or $this->Time->format()

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
  • Would like to add that `Cake\I18n\FrozenTime` is a valid argument to [`\Datetime::diff()`](https://www.php.net/manual/en/datetime.diff.php) as well. – ᴍᴇʜᴏᴠ Oct 01 '20 at 08:52
2

In boostrap.php add

Cake\I18n\FrozenDate::setToStringFormat('yyyy-MM-dd');

still it comes with forzenDate object with same params But when you will print in view then it will print the proper format

echo $var->created;  // print: 2016-08-01

Reference for Dates Datetime Format Syntax

Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
tarikul05
  • 1,843
  • 1
  • 16
  • 24
2

You can also use TimeHelper for formating datetime in View

Example

echo $this->Time->format(
  $YourDateTimeVariable, #Your datetime variable
  'Y-MM-d'               #Your custom datetime format
);

CakePHP TimeHelper function details is Here

Sumon Sarker
  • 2,707
  • 1
  • 23
  • 36
0

You can directly print the date object in any custom date format by using inbuilt i18nFormat function.

$frozenDateObj->i18nFormat('dd-MMM-yyyy');

Use datetime-syntax reference for more customization

Irshad Khan
  • 5,670
  • 2
  • 44
  • 39