简体   繁体   English

在cakephp中格式化日期字符串

[英]formatting date string in cakephp

I'm new to cakephp, I've got a simple Users controller that corresponds to a users table. 我是cakephp的新手,我有一个简单的用户控制器,对应一个用户表。 I have a created field in the table that I want to ouput on the view action using the niceShort() function. 我在表中创建了一个字段,我希望使用niceShort()函数输出视图操作。 how do I use it in the view? 我如何在视图中使用它?

Current code is: 目前的代码是:

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

thanks, 谢谢,

Jonesy Jonesy

In the controller you can include the build in time helper: 在控制器中,您可以包含构建时间助手:

users_controllee.php: 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. 我认为darko是对的。

You can simply use PHP function date() to format your date in any type. 您可以使用PHP函数date()以任何类型格式化日期。

Example : 示例:

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

Here, strtotime() is the function of cakePHP to convert in datetime format. 这里,strtotime()是cakePHP以datetime格式转换的函数。

Now you will have $date variable with a date formatted 'YYYY-mm-dd Hour:Minute:Second'. 现在,您将拥有$ date变量,其日期格式为'YYYY-mm-dd Hour:Minute:Second'。

For more option you can refer to PHP date manual : http://php.net/manual/en/function.date.php 有关更多选项,请参阅PHP日期手册: http//php.net/manual/en/function.date.php

Hope this will be helpful to you... 希望这会对你有所帮助......

Just use built in php function date. 只需使用内置的php函数日期。

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 http://php.net/manual/en/function.date.php

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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