Benoda
Benoda

Reputation: 133

CakePHP 3 - Displaying time without date

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.

Upvotes: 1

Views: 1455

Answers (2)

ndm
ndm

Reputation: 60453

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.

Upvotes: 2

chris85
chris85

Reputation: 23892

According to the documentation it should be:

$event->date->format('h:i A');

Where i is the minutes and A is AM or PM. This is using PHP's date's format, see http://php.net/manual/en/function.date.php for additional usages/formats.

Upvotes: 2

Related Questions