【发布时间】:2011-11-04 10:29:19
【问题描述】:
我想将 created_at 字段日期从原始格式格式化为 03.May.2011 之类的内容,以便在 indexSuccess.php 和 showSuccess.php 中显示
你可以帮帮我吗?
谢谢
【问题讨论】:
标签: date symfony1 format symfony-1.4
我想将 created_at 字段日期从原始格式格式化为 03.May.2011 之类的内容,以便在 indexSuccess.php 和 showSuccess.php 中显示
你可以帮帮我吗?
谢谢
【问题讨论】:
标签: date symfony1 format symfony-1.4
你可以在你的 indexSuccess.php 和 showSuccess.php 中使用 symfony 来代替例如:
<?php $value->getCreatedAt() ?>
下一个:
<?php echo date('d.M.Y', strtotime($value->getCreatedAt())) ?>
您可以使用other 格式。
【讨论】:
某些数据的格式绝对不属于控制器上下文 - 所以请使用
use_helper("date");
echo format_date($myDate);
来自模板中的 symfony 日期助手(showSuccess.php、blaSuccess.php)或部分(form.php、list.php、test.php)!
您可以在此处http://www.symfony-project.org/gentle-introduction/1_4/en/13-I18n-and-L10n#chapter_13_sub_outputting_data_in_the_user_s_culture 或源文件中找到更多信息。
【讨论】:
<?php echo $sgps_breves->getCreatedAt() ?> 我应该写use_helper("date") echo format_date($sgps_breves->getCreatedAt())?
我相信日期以字符串格式Y-m-d H:i:s 返回,适合 MySQL 日期时间类型。最好使用 DateTime::createFromFormat 将其转换为 PHP DateTime 实例,假设您使用的是 PHP > 5.3。
所以,在你的控制器中:
$this->creation_date = DateTime::createFromFormat('Y-m-d H:i:s', $item->created_at);
那么,在你看来:
<?php echo $creation_date->format('d.F.Y') ?>
如果您使用的是 PHP 5.2 或更早版本,则不能使用createFromFormat。你可能想回到strtotime:
$this->creation_date = strtotime($this->created_at);
在视图中:
<?php echo date('d.F.Y', $creation_date) ?>
【讨论】:
actions.class.php,在您模块的actions 文件夹中。
action.class.php 的executeIndex 函数中,它得到了这个错误:Fatal error: Call to undefined method DateTime::createfromformat() in C:\wamp\www\sgps\apps\frontend\modules\breves\actions\actions.class.php on line 18
要创建一个日期字符串,如您的示例 (03.May.2011),请使用以下方式:
<?php use_helper("date"); ?>
<?php echo format_date($myDate,'dd. MMM. yyyy'); ?>
【讨论】: