php - Codeigniter - 在将其发送到 View 之前处理从数据库检索的值

标签 php mysql database codeigniter-2

我想使用 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 文件 views/content/home.php 中使用 date() 函数 函数在 View 之外,不确定是否应该在模型或 Controller 中完成。

有什么建议吗?

最佳答案

您可以在 Controller 类中完成它。首先创建一个私有(private)函数名称 DateProcess 并发送查询结果并根据需要处理日期。然后您可以将过程数据发送到查看文件。

关于php - Codeigniter - 在将其发送到 View 之前处理从数据库检索的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11934584/

相关文章:

php - 查询生成器中的 Symfony Doctrine if 语句

Android Room - 在查询中使用外键

php - wordpress 如何将自定义字段条目与数据库表中的值进行比较?

php - 即使安装了Laravel-homestead也找不到Mysql

php - 通过连接从两个表中选择数据

MySql 添加带有格式的时间戳列

javascript - 将unicode php变量传递给javascript

php - 当我在内部使用 _GET 时,MySql 数据库查询不起作用

java - Android SQLLiteDataBase 游标返回 0 行

php - 如何在 Laravel 中使用多个数据库