php 如何使用 CodeIgniter 格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8125274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format a date with CodeIgniter
提问by Jeff Davidson
I'm trying to figure out what I'm doing wrong here. I'm wanting to format the date_published
field in my query and I'm getting a t_string syntax error
in my IDE.
我试图弄清楚我在这里做错了什么。我想格式化date_published
查询中的字段,并且t_string syntax error
在我的 IDE 中得到了一个。
$this->db->select('site_news_articles.article_title, site_news_articles.is_sticky,' date_format('site_news_articles.date_published, 'f jS, Y')');
UPDATE:
更新:
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();
}
Error Number: 1064
错误编号:1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (site_news_articles
) WHERE site_news_articles
.news_category_id
= 2 A' at line 2
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'FROM( site_news_articles
) WHERE附近使用的正确语法site_news_articles
。news_category_id
= 2 A' 在第 2 行
SELECT 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_articles
) WHERE site_news_articles
.news_category_id
= 2 AND site_news_articles
.is_approved
= 'Yes' AND site_news_articles
.status_id
= 1 ORDER BY site_news_articles
.date_published
desc LIMIT 10
选择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_articles
) WHERE site_news_articles
. news_category_id
= 2 AND site_news_articles
. is_approved
= 'Yes' AND site_news_articles
. status_id
= 1 ORDER BY site_news_articles
. date_published
desc LIMIT 10
Filename: /home/xtremer/public_html/models/sitemodel.php
文件名:/home/xtremer/public_html/models/sitemodel.php
Line Number: 140
行号:140
回答by swatkins
You're trying to execute PHP withina mysql query. You need to be using the MySQL DATE_FORMAT
function. Take a look here:
您正在尝试在mysql 查询中执行 PHP 。您需要使用 MySQLDATE_FORMAT
函数。看看这里:
http://davidwalsh.name/format-date-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);
the $this->db->select()
method is used to 'generate' a sql query - so trying to use the CI date_format
function there, will not generate the correct sql query that will be executed on your database.
该$this->db->select()
方法用于“生成”一个 sql 查询 - 因此尝试在date_format
那里使用 CI函数,将不会生成将在您的数据库上执行的正确 sql 查询。
You can either return the raw date with your query and then format the date with CI, or you need to use the MySQL DATE_FORMAT
function to return the formatted date with mysql.
您可以使用查询返回原始日期,然后使用 CI 格式化日期,或者您需要使用 MySQLDATE_FORMAT
函数通过 mysql 返回格式化的日期。
回答by minboost
Looks like you have a syntax error.
看起来你有一个语法错误。
$this->db->select("site_news_articles.article_title, site_news_articles.is_sticky, DATE_FORMAT(site_news_articles.date_published, '%M %D, %Y')", FALSE);
Whenever you're building a query in CI, you should used double quotes to surround it, so you can use the single quotes in your query without having to escape them.
每当您在 CI 中构建查询时,都应该使用双引号将其括起来,这样您就可以在查询中使用单引号而不必对它们进行转义。
回答by orvyl
try to use mdate:
尝试使用 mdate:
echo mdate('%M %d, %Y at exactly %h:&i %a',strtotime(date));
回答by Ragnar123
Looks like you forgot a single quote
看起来你忘记了单引号
$this->db->select("site_news_articles.article_title, site_news_articles.is_sticky, date_format(site_news_articles.date_published, 'f jS, Y')");
回答by Eruant
An alternative would be to use php's date format
另一种方法是使用 php 的日期格式
date(string format [, int timestamp])
So...
所以...
date('Y-m-d h:i:s', 'site_news_articles.date_published')