Convert time object of cakephp 3 in Y-m-d format

2020-07-27 02:04发布

I am working in cakephp 3 and I want to print my time object in Y-m-d format.

This is my object

'expiry' => object(Cake\I18n\Time) {
        'time' => '2015-07-31T00:00:00+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false
},

Can anyone help me with this?

4条回答
一纸荒年 Trace。
2楼-- · 2020-07-27 02:45

In Cakephp 3.* you use the Time::i18nFormat() method. It worked for me

  $customformat = $date->i18nFormat('YYY-MM-dd');

Edit:

I looked through the 3.3 docs and found this good example. There are constants you can use to format dates for common use cases like Time::UNIX_TIMESTAMP_FORMAT,\IntlDateFormatter::FULL

$time = new Time('2014-04-20 22:10');
$time->i18nFormat(); // outputs '4/20/14, 10:10 PM' for the en-US locale
$time->i18nFormat(\IntlDateFormatter::FULL); // Use the full date and time format
$time->i18nFormat([\IntlDateFormatter::FULL, \IntlDateFormatter::SHORT]); // Use full date but short time format
$time->i18nFormat('yyyy-MM-dd HH:mm:ss'); // outputs '2014-04-20 22:10'
$time->i18nFormat(Time::UNIX_TIMESTAMP_FORMAT); // outputs '1398031800'

CakePHP documentation

Edit 2:

Also look at the time helper class

You can reference it in views like so:

$this->Time->format($format);

查看更多
干净又极端
3楼-- · 2020-07-27 02:46

$object->created->format('Y-m-d')

查看更多
一纸荒年 Trace。
4楼-- · 2020-07-27 02:51

get result where dt_updated is less than current date.

$result = $this->ModelTable->find('all');
$result->where(['DATE(dt_updated) <'=>date('Y-m-d')]);
查看更多
迷人小祖宗
5楼-- · 2020-07-27 03:00

I did it. All I had to do was use the Time helper in my view. $this->Time->format($user->expiry,'Y-M-d').

查看更多
登录 后发表回答