Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting date string in cakephp

I'm new to cakephp, I've got a simple Users controller that corresponds to a users table. I have a created field in the table that I want to ouput on the view action using the niceShort() function. how do I use it in the view?

Current code is:

<p>Member since <?php echo $user['User']['created']?></p>

thanks,

Jonesy

like image 676
iamjonesy Avatar asked Aug 19 '10 10:08

iamjonesy


People also ask

How can I get current date and time in cakePHP?

use Cake\I18n\Time; // Create from a string datetime. $time = Time::createFromFormat( 'Y-m-d H:i:s', $datetime, 'America/New_York' ); // Create from a timestamp $time = Time::createFromTimestamp($ts); // Get the current time.


2 Answers

In the controller you can include the build in time helper:

users_controllee.php:

var $helpers = array('Time');

In the view:

<p>Member since <?php echo $time->niceShort($user['User']['created']); ?></p>
like image 103
iamjonesy Avatar answered Sep 25 '22 23:09

iamjonesy


Just use built in php function date.

You can use it like this:

echo date('d.m.Y', strtotime($user['User']['created']));

You can use any format you like for date formatting based on build in patterns.

http://php.net/manual/en/function.date.php

like image 36
darpet Avatar answered Sep 22 '22 23:09

darpet