php 在 cakephp 中格式化日期字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3520982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
formatting date string in cakephp
提问by iamjonesy
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?
我是 cakephp 的新手,我有一个与用户表相对应的简单用户控制器。我在表中有一个创建的字段,我想使用 niceShort() 函数在视图操作上输出该字段。我如何在视图中使用它?
Current code is:
当前代码是:
<p>Member since <?php echo $user['User']['created']?></p>
thanks,
谢谢,
Jonesy
琼斯
回答by iamjonesy
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>
回答by Kapil
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("Y-m-d H:i:s", strtotime($user['User']['created']));
$date = date("Y-m-d H:i:s", strtotime($user['User']['created']));
Here, strtotime() is the function of cakePHP to convert in datetime format.
这里的 strtotime() 是 cakePHP 转换为日期时间格式的函数。
Now you will have $date variable with a date formatted 'YYYY-mm-dd Hour:Minute:Second'.
现在您将拥有日期格式为“YYYY-mm-dd Hour:Minute:Second”的 $date 变量。
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...
希望这对你有帮助...
回答by darpet
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.
您可以根据内置模式使用您喜欢的任何格式进行日期格式化。
回答by Sam D
Just as point of reference the TimeHelper is CakePHP has a lot of nice stuff worth looking at http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
正如 TimeHelper 的参考点一样,CakePHP 有很多值得一看的好东西 http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html