【问题标题】:How to change mysql date format using codeigniter如何使用codeigniter更改mysql日期格式
【发布时间】:2013-01-04 13:43:56
【问题描述】:

我正在使用 CI 和 mysql 开发一个网站。我想将日期格式从 yyyy-mm-dd 更改为 dd-mm-yyyy。我知道我应该使用 date_format 函数。但是当我尝试使用它时,它不起作用——日期格式没有改变。我也尝试添加第二个参数(FALSE)。但是,我也发现了问题,特别是当我需要查询超过 1 列时。这是我的代码:

function __construct() {
    parent::__construct();
    $this->table = array(
        'name'      => 'article',
        'coloumn'   => array(
            'article_id',
            'article_title',
            'article_content',
            'article_summary',
            'date_format(article_publishdate, \'%d-%M-%Y %H:%i:%s\') as article_publishdate',
            'article_source',
            'article_link',
            'media_type',
            'media_link',
            'media_position',
            'article_category.category_title',
        ),
        'join'      =>  array(
                            0 => array('article_category' , 'article.article_category_id=article_category.category_id', 'left'),
                        ),    
        'where'     => array(),    
        'order'     => array(),
        'limit'     => array(),
        'idkey'     => 'article_id',
    );  
}

public function getfullbody($id)
{
    $this->db->query("update article set article_view = (article_view+1) where article_id = '$id'");
    $this->db->select($this->table['column'], FALSE);
        if (count($this->table['join'])>0){
            foreach ($this->table['join'] as $row):
                if (!empty($row[2])){
                    $this->db->join($row[0], $row[1], $row[2]);
                }
                else {
                    $this->db->join($row[0], $row[1]);
                }
            endforeach;
        }

    $this->db->where("article_id = '$id'");
    $query = $this->db->get($this->table['name']);
    $data = $query;
    return $data;
}

问题:当我将 date_format() 与 codeigniter 一起使用时,它并没有改变日期格式。 那么,如何修复呢?谢谢。

【问题讨论】:

  • 使用 date_format 时发生了什么?这对我来说每次都像魅力一样。格式有一些错误,例如您想要 mm 格式的月份,但您使用的是返回月份名称的 M。请再次参考。 dev.mysql.com/doc/refman/5.5/en/…
  • 当我使用 CI 时,该函数没有改变格式。但是当我在没有 CI 的情况下使用它时,它就可以了。
  • 你打错了..你定义$this->table['coloumn']而不是$this->table['column']

标签: php mysql codeigniter date-format


【解决方案1】:

将coloumn更改为列索引,如果您想将日期显示为dd-mm-yyyy,请使用以下代码

function __construct() {
    parent::__construct();
    $this->table = array(
        'name'      => 'article',
        'column'   => array(
            'article_id',
            'article_title',
            'article_content',
            'article_summary',
            'date_format(article_publishdate, \'%d-%m-%Y\') as article_publishdate',
            'article_source',
            'article_link',
            'media_type',
            'media_link',
            'media_position',
            'article_category.category_title',
        ),
        'join'      =>  array(
                            0 => array('article_category' , 'article.article_category_id=article_category.category_id', 'left'),
                        ),    
        'where'     => array(),    
        'order'     => array(),
        'limit'     => array(),
        'idkey'     => 'article_id',
    );  
}

public function getfullbody($id)
{
    $this->db->query("update article set article_view = (article_view+1) where article_id = '$id'");
    $this->db->select($this->table['column'], FALSE);
        if (count($this->table['join'])>0){
            foreach ($this->table['join'] as $row):
                if (!empty($row[2])){
                    $this->db->join($row[0], $row[1], $row[2]);
                }
                else {
                    $this->db->join($row[0], $row[1]);
                }
            endforeach;
        }

    $this->db->where("article_id = '$id'");
    $query = $this->db->get($this->table['name']);
    $data = $query;
    return $data;
}

【讨论】:

    【解决方案2】:

    测试代码点火器 2...

    ('%d-%m-%Y')
    

    在你身边

    "date_format(article_publishdate, ('%d-%m-%Y')) as article_publishdate",
    

    这对我来说非常有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多