Get 12 hour time from Time Helper in CakePHP
I'm using the niceSho开发者_开发问答rt
function in the Time Helper in Cake to display some times from my database. It works wonderfully, but it would be great to be able to have niceShort
use 12 hour time instead of 24. Is there a way to do this without modifying the helper?
To make a 12-hour time, simply do this in your view:
<?php echo $time->format('g:la', $string); ?>
For a list of all of the different formatting options, see:
http://php.net/manual/en/function.date.php
In other words, my above example (g:la)
would output 4:45pm
I still wanted to be able to use the nice
and niceShort
functions, so I modified the Time helper and moved it into app/views/helpers.
.
Use the format
function and provide the date/time format you want. It is the same format string as the date
function in PHP.
As cakephp's format() uses PHP's strftime() formatting options. You can use
%I:%M %p or %r
echo $this->Time->format($news['News']['modified'],'%d/%m/%Y %I:%M %p');
it will format for example: 21:34:17 to 09:34:17 PM. You can read more here.
精彩评论