Jump to content

[SOLVED] sort results by date


bigrossco

Recommended Posts

If your field is actually a date field, which it should be, it will sort by itself. If you are storing the dates like you said, you shouldn't. You should store dates AS dates then format them the way you like, either with the sql statement or with php. Then the dates will sort correctly.

Ray
By all means, craygo is right on... You're best off changing your data type to DATE and using it as intended; however, if your database is one in which you cannot easily change your structure, you can do something like this:
[code]
SELECT SUBSTRING(myDate, 7) AS year, SUBSTRING(myDate, 4, 2) AS month, SUBSTRING(myDate, 1, 2) AS day
FROM tableName
ORDER BY year DESC, month DESC, day DESC;
[/code]

Basically, you're going to have to split your date apart and sort by year, month and then day. This can be extremely resource intensive if you have a ton of records since you're actually doing a [b]lot[/b] of string manipulation with each and every record. You'll be much better off in the long run changing your data types, but this may help you as well.

Good luck.
I would suggest you use micro time, and use sort() for accending that is the earliest goes top and read to the bottom,the last one will be the latest. OR rsort() which is reverse sort, ie. latest goes top earilest goes to bottom, BOTH OF THEM NEEDS TO PUT TIME INTO AN ARRAY BEFORE THE SORT.
By micro time, even if two users submit at nearly the same time, it will be able to diffrentiate...
Ted, hope this advice is useful.
[quote author=bigrossco link=topic=120124.msg492553#msg492553 date=1167315162]
I store it correctly in the database, I have made the coding for the result to show in php the correct way i.e. dd/mm/yyyy
[/quote]
Well, if it's stored as a DATE field, all you have to do is "ORDER BY myDateField DESC" to get them to sort by the most recent date first.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.