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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.