问题
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