How to format the time in CakePHP?
I am using CakePHP 1.3 to write a application, and I create a datetime
field in a database.
And I call it echo $post['Post']['created'];
and I get something like 2011-07开发者_Go百科-03 00:00:00
.
What I would like is for it to look more like Jan 1st 2008, 19:25
.
I see some documentation in 7.12.1 Formatting (CakePHP manual), but how do implement it?
To do this using the built in cake Time
helper, use this:
// controller:
$helpers = array('Time');
then in your view:
echo $this->Time->nice($post['Post']['created']);
// outputs Tue, Jan 1st 2008, 19:25".
If you're looking for performance (although negligible), you would be better off using date
directly as the other poster suggests, without having the overhead of loading the Time
helper.
But the helper is easier and quicker to implement, not to mention opens up the other helper functions, so it will depend on your needs obviously.
You need to the format the date using date()
. See http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.strtotime.php
echo date('M j Y, h:i', strtotime($post['Post']['created']));
A better solution would be to create a helper. The best place to learn about these is the CakePHP manual.
The easiest way to do it in Cake is to use the time helper. The previous post give the correct answer for CakePHP 2.x, you however asked for a solution for 1.3. In your controller write:
var $helpers = array('Time');
In your view write:
echo $time->niceShort($post['Post']['created']);
This has worked for me the last 3 years.
精彩评论