Cakephp 3 giving date and time fields in frozentime object

10.1k views Asked by At

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
)
4

There are 4 answers

1
tarikul05 On

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

0
Sumon Sarker On

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

1
ᴍᴇʜᴏᴠ On

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()

0
Irshad Khan On

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