rrd
rrd

Reputation: 1451

cakephp 3 displaying date without time

CakePHP 3: I have a database field which is a DATE (not DATETIME nor TIMESTAMP)

When I display

echo $contact->date;

It will show something like 2014. 01. 06. 0:00. How to hide hours and minutes?

I tried

print $this->Time->format($contact->date, 'Y-m-d');

but I got 2014-0-6

How to get year-month-day?

rrd

Upvotes: 9

Views: 10455

Answers (7)

Devi Sahoo
Devi Sahoo

Reputation: 21

use Cake\I18n\FrozenTime;

$time =new FrozenTime('2021-01-31 22:11:30');
$timex =  $time->i18nFormat('yyyy-MM-dd');
echo $timex;

Upvotes: 1

Irshad Khan
Irshad Khan

Reputation: 6066

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

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

Use Datetime Format Syntax reference for more customization

Upvotes: 1

Debendra
Debendra

Reputation: 144

echo date_format($contact->date, 'Y-m-d'); //php format

Upvotes: 0

tarikul05
tarikul05

Reputation: 1913

If need all over the project with specific format you can use
boostrap.php

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

Upvotes: 0

edward M.
edward M.

Reputation: 51

add in App\Controller:

use Cake\I18n\Time;
Time::$defaultLocale = 'es-ES';
Time::setToStringFormat('YYYY-MM-dd');

Upvotes: 5

Massi
Massi

Reputation: 74

try

date('Y-m-d',strtotime($contact->date));

Upvotes: -1

conFusl
conFusl

Reputation: 939

Have you tried this?

echo $contact->date->format('Y-m-d');

Upvotes: 18

Related Questions