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

♀尐吖头ヾ 提交于 2019-12-10 11:31:51

问题


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?


回答1:


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




回答2:


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




回答3:


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




回答4:


get result where dt_updated is less than current date.

$result = $this->ModelTable->find('all');
$result->where(['DATE(dt_updated) <'=>date('Y-m-d')]);


来源:https://stackoverflow.com/questions/31452882/convert-time-object-of-cakephp-3-in-y-m-d-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!