Jay
Jay

Reputation: 510

Get 12 hour time from Time Helper in CakePHP

I'm using the niceShort 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?

Upvotes: 2

Views: 875

Answers (4)

RN Kushwaha
RN Kushwaha

Reputation: 2136

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.

Upvotes: 0

Jay
Jay

Reputation: 510

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.

.

Upvotes: 1

Joseph
Joseph

Reputation: 2712

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

Upvotes: 1

j.w.r
j.w.r

Reputation: 4249

Use the format function and provide the date/time format you want. It is the same format string as the date function in PHP.

Upvotes: 0

Related Questions