PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] CakePHP 3 - Displaying time without date

 January 17, 2022     cakephp, cakephp-3.0, datetime, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing