CakePHP Time Helper: how to use it in a view
First of all, in the controller, add it to the $helpers array:
var $helpers = array(‘Html’, ‘Form’, ‘Time’);
Then in the corresponding view, call it like this:
<p>Date Published: <?=$time->nice($post['Post']['date']);?></p>
which effectively (in this case from the database in this example) turns this: 2010-01-02 21:54:00
into this: Sat, Jan 2nd 2010, 21:54
—- note: —-
without the Time helper we could of course do this specific thing with the plain ‘ol PHP date function this way:
<?=date(‘M jS Y, g:i a’,strtotime($post['Post']['date']));?>
—-
$post['Post']['date'] is the value of the object Time will beautify, in this case
sent by the following function in the controller:
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__(‘Invalid Post.’, true));
$this->redirect(array(‘action’=>’index’));
}
$this->set(‘post’, $this->Post->read(null, $id));
}