tags:

views:

38

answers:

2

Right now im sorting by each articles auto_increment id with the query below

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.

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'];
}
+4  A: 

Just change the column in the ORDER BY:

SELECT * FROM articles ORDER BY time DESC
Mark Byers
Wow Im a retard lol, thanks
kr1zmo
+2  A: 

Let MySQL handle the date stuff - IMO its MUCH better at it than 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)

your query should be:

$alist = mysql_query("SELECT * FROM articles ORDER BY `your_date_field` DESC");
ToonMariner