Monday, January 17, 2022

[FIXED] CakePHP 3 - Displaying time without date

Issue

CakePHP 3.

I have a database field which is DATETIME. What I want is to show the time without the date.

Example:

Field value: 2015-09-08 07:27:12.000000

What I want to show: 7:27 AM

I tried:

<?= $event->date->format('HH:mm') ?>

I got 07:09 (Hour-Month).

OR

<?= $this->Time->format($event->date, [null, \IntlDateFormatter::SHORT]) ?>

I got Tuesday, September 8, 2015 at 7:27 AM.

I would like to know how to get Hours-Minutes AM/PM.


Solution

null is not an acceptable formatting style, instead use \IntlDateFormatter::NONE to suppress parts of the date.

$this->Time->format($event->date, [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT]);

or

$event->date->i18nFormat([\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT]);

Given that your application uses a suitable locale (like en_US), this should leave you with your desired time format.



Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.