【问题标题】:How to format a date with CodeIgniter如何使用 CodeIgniter 格式化日期
【发布时间】:2011-11-14 17:08:13
【问题描述】:

我想弄清楚我在这里做错了什么。我想在我的查询中格式化 date_published 字段,并且在我的 IDE 中得到一个 t_string syntax error

$this->db->select('site_news_articles.article_title, site_news_articles.is_sticky,' date_format('site_news_articles.date_published, 'f jS, Y')');

更新:

function getNewsTitles($category_id) {
    $this->db->select('site_news_articles.article_title, site_news_articles.is_sticky');
    $this->db->select("DATE_FORMAT(site_news_articles.date_published, '%M %e, %Y') as formatted_date", TRUE);
    $this->db->from('site_news_articles');
    $this->db->where('site_news_articles.news_category_id', $category_id); 
    $this->db->where('site_news_articles.is_approved', 'Yes');
    $this->db->where('site_news_articles.status_id', 1);
    $this->db->order_by('site_news_articles.date_published', 'desc');  
    $this->db->limit(10);
    $query = $this->db->get();
    return $query->result_array(); 
}

错误号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的 'FROM (site_news_articles) WHERE site_news_articles.news_category_id = 2 A' 附近使用正确的语法

选择site_news_articles.article_title, site_news_articles.is_sticky, DATE_FORMAT(site_news_articles.date_published, '%M %e, %Y') as formatted_date FROM (site_news_articles336@98765436@@987645) @ = 2 AND site_news_articles.is_approved = '是' AND site_news_articles.status_id = 1 ORDER BY site_news_articles.date_published desc LIMIT 10

文件名:/home/xtremer/public_html/models/sitemodel.php

行号:140

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您似乎有语法错误。

    $this->db->select("site_news_articles.article_title, site_news_articles.is_sticky, DATE_FORMAT(site_news_articles.date_published, '%M %D, %Y')", FALSE);
    

    每当您在 CI 中构建查询时,都应该使用双引号将其括起来,这样您就可以在查询中使用单引号而无需对其进行转义。

    【讨论】:

    • 抱歉,我修复了代码以使用 MySQL DATE_FORMAT() 函数的正确参数。
    • CI 可能会通过尝试转义字段名称来破坏代码。我更新了代码以将第二个参数作为 FALSE 传递给 db->select() 函数
    • 我会通过在末尾添加一个 AS 子句来改进这一点,否则在主代码中处理该值是丑陋的。$this->db->select("site_news_articles.article_title, site_news_articles.is_sticky, DATE_FORMAT (site_news_articles.date_published, '%M %D, %Y') AS human_date", FALSE);
    • 我们为什么要使用 false/
    【解决方案2】:

    您正在尝试在 mysql 查询中执行 PHP 。您需要使用 MySQL DATE_FORMAT 函数。看这里:

    http://davidwalsh.name/format-date-mysql-date_format

    $this->db->select('site_news_articles.article_title, site_news_articles.is_sticky');
    $this->db->select("DATE_FORMAT(site_news_articles.date_published, '%M %e, %Y') as formatted_date", FALSE);
    

    $this->db->select() 方法用于“生成”一个 sql 查询 - 因此尝试在那里使用 CI date_format 函数,不会生成将在您的数据库上执行的正确 sql 查询。

    您可以使用查询返回原始日期,然后使用 CI 格式化日期,或者您需要使用 MySQL DATE_FORMAT 函数使用 mysql 返回格式化的日期。

    【讨论】:

    • 糟糕,我在 select 方法中传递了 true 而不是 false。对于那个很抱歉。更新了我的帖子。
    【解决方案3】:

    尝试使用 mdate:

    echo mdate('%M %d, %Y at exactly %h:&i %a',strtotime(date));
    

    【讨论】:

      【解决方案4】:

      您好像忘记了单引号

      $this->db->select("site_news_articles.article_title, site_news_articles.is_sticky, date_format(site_news_articles.date_published, 'f jS, Y')");
      

      【讨论】:

      • 唯一的问题是它现在说...消息:date_format() 期望参数 1 是 DateTime,给定的字符串很奇怪,因为在我的数据库中,该字段是 datetime 数据类型字段
      【解决方案5】:

      另一种方法是使用 php 的日期格式

      date(string format [, int timestamp])
      

      所以...

      date('Y-m-d h:i:s', 'site_news_articles.date_published')
      

      【讨论】:

      • 这在查询中不起作用 - 仅在 PHP 中。他正在构建查询,而不是格式化输出。
      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 2021-05-14
      • 1970-01-01
      • 2015-05-18
      • 2011-10-06
      • 1970-01-01
      相关资源
      最近更新 更多