我想使用 PHP 函数 date()
来显示从数据库捕获的时间戳值,以正确格式化我的日期时间。
但问题是 - 可以在将其传递到 View 文件之前完成吗?
关于 View 文件内的 php 函数,我希望使 View 文件尽可能干净。
使用/models/content_model.php
从数据库检索项目:
function getPages($per_page){
$this->db->order_by('id', 'desc');
$query = $this->db->get('content', $per_page, $this->uri->segment(3));
return $query->result();
}
然后由 Controller /controllers/content.php
处理:
...
// Get data
$componentData['results'] = $this->content_model->getPages(5);
...
并在views/content/home.php
中显示结果:
<?php
<?php foreach($results as $item):?>
echo $item->date_created;
<?php endforeach;?>
?>
将输出一个日期(在数据库中输入时间戳),如下所示:2011-08-14 18:34:04
我想通过 date() 处理它,而不是在 View 文件
函数在 View 之外,不确定是否应该在模型或 Controller 中完成。views/content/home.php
中使用 date()
函数
有什么建议吗?
最佳答案
您可以在 Controller 类中完成它。首先创建一个私有(private)函数名称 DateProcess 并发送查询结果并根据需要处理日期。然后您可以将过程数据发送到查看文件。
关于php - Codeigniter - 在将其发送到 View 之前处理从数据库检索的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11934584/