php mysql按日期排序(最新)

时间:2021-02-07 17:07:22

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'];
}

2 个解决方案

#1


16  

Just change the column in the ORDER BY:

只需更改ORDER BY中的列:

SELECT * FROM articles ORDER BY time DESC

#2


4  

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");

#1


16  

Just change the column in the ORDER BY:

只需更改ORDER BY中的列:

SELECT * FROM articles ORDER BY time DESC

#2


4  

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");