简体   繁体   中英

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

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>

I think darko is right.

You can simply use PHP function date() to format your date in any type.

Example :

$date = date("Ymd H:i:s", strtotime($user['User']['created']));

Here, strtotime() is the function of cakePHP to convert in datetime format.

Now you will have $date variable with a date formatted 'YYYY-mm-dd Hour:Minute:Second'.

For more option you can refer to PHP date manual : http://php.net/manual/en/function.date.php

Hope this will be helpful to you...

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

作为参考点,TimeHelper是CakePHP有很多值得关注的好东西http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM