php mysql 按日期排序(最新)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3376458/
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
php mysql sort by date (newest)
提问by kr1zmo
Right now im sorting by each articles auto_increment id with the query below
现在我按每篇文章 auto_increment id 排序,下面的查询
mysql_query("SELECT * FROM articles ORDER BY id DESC");
mysql_query("SELECT * FROM articles ORDER BY id DESC");
I wanna know how to sort by a date field i made, that stores the current date via, strtotime() it should query the dates from newest to oldest.
我想知道如何按我制作的日期字段排序,它通过 strtotime() 存储当前日期,它应该查询从最新到最旧的日期。
Current code
当前代码
$alist = mysql_query("SELECT * FROM articles ORDER BY id DESC");
$results = mysql_num_rows($alist);
if ($results > 0){
while($info = mysql_fetch_array($alist)) {
// query stuff
echo $info['time'];
}
回答by Mark Byers
Just change the column in the ORDER BY:
只需更改 ORDER BY 中的列:
SELECT * FROM articles ORDER BY time DESC
回答by Ian Wood
Let MySQL handle the date stuff - IMO its MUCH better at it than PHP...
让 MySQL 处理日期的事情 - IMO 比 PHP 更擅长它......
add a column with DATE or DATETIME type in your table. On inserting a new record either use NOW() or set a trigger to do it for you (will have to allow null in the coulmn if you are going to user a trigger)
在表中添加一个 DATE 或 DATETIME 类型的列。在插入新记录时,使用 NOW() 或设置触发器为您执行此操作(如果您要使用触发器,则必须在 coulmn 中允许 null)
your query should be:
您的查询应该是:
$alist = mysql_query("SELECT * FROM articles ORDER BY `your_date_field` DESC");

