[SOLVED]Sorting on date?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Diod
Forum Commoner
Posts: 52
Joined: Tue Oct 19, 2004 9:07 am

[SOLVED]Sorting on date?

Post by Diod »

I dont know if this is the right categorie to ask,

but how do i sort on date (oldest as last and newest as first)

when $date is for my date and $row["time"] is for my time ?
Last edited by Diod on Tue Oct 19, 2004 12:25 pm, edited 2 times in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Append this to your mysql query:

Code: Select all

order by time desc
This man page might help you out:
http://dev.mysql.com/doc/mysql/en/Sorting_rows.html
User avatar
Diod
Forum Commoner
Posts: 52
Joined: Tue Oct 19, 2004 9:07 am

Post by Diod »

thx :)

and if i want to order by date AND by time?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the last example in his link.
User avatar
Diod
Forum Commoner
Posts: 52
Joined: Tue Oct 19, 2004 9:07 am

Post by Diod »

if i understand it correctly, i just do

Code: Select all

$result = mysql_query("SELECT * FROM news order by date, time desc",$db);


EDIT: it works, even without order by time..

it seems to order by date AND time even if i only use date
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

At first i also used 2 fields, however if you have multiple time and dates in your database like i did double the fields.. For instance i had 3 fields for time and 3 fields for date so basically 6 fields, i changed that to 3 fields.

And did an order by date,'dd-mm-yyyy hh:mm:ss' desc (european notation).. :D

Looked around at the mysql help and you can filter out all the values for day, month, year with something like %m or so.. Have a look at the manual for the exact stripping... :D

Hope this helps a bit.. :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I believe dreamline is referring to [mysql_man]extract[/mysql_man]
Post Reply