Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > MySQL Time sorting question

MySQL Time sorting question
Thread Tools
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Aug 3, 2004, 12:57 PM
 
Hi,

I have a table with one of the fields being a string with the name of "time". It contains times throughout one day in the format of things like:

8:55 AM Eastern Time
9:00 AM
9:00 AM
9:00 AM
10:15 AM
11:10 AM
12:00 PM

Notice there can be multiplies. Also, they times are not in that nice order. My question is, how would I make it (using either PHP or MySQL) to sort the time string in chronological order. Keep in mind that 10:10 PM is later then 11:00 AM.

THanks!
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Aug 3, 2004, 01:24 PM
 
The best solution would be to convert the MySQL field to a real time field (stored as a timestamp) and then just format it back to whatever format you would want for display. Then you could simply put in a 'ORDER BY <field name> ASC'. But it sounds like you have a time string stored as a text field (*boo* *hiss*).

So the second option is to try and use PHP's strtotime function. This converts things to a timestamp (number of seconds since the epoc... whatever that happens to be on your system). Then you just need to compare the numbers to sort them. Hope that works for you, because otherwise it will be a tough slog to get the job done.
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Aug 3, 2004, 06:23 PM
 
Originally posted by timmerk:
Hi,

I have a table with one of the fields being a string with the name of "time". It contains times throughout one day in the format of things like:

8:55 AM Eastern Time
9:00 AM
9:00 AM
9:00 AM
10:15 AM
11:10 AM
12:00 PM

Notice there can be multiplies. Also, they times are not in that nice order. My question is, how would I make it (using either PHP or MySQL) to sort the time string in chronological order.
Indeed. Alter your table to make that field a TIME or DATETIME field which will store it as HH:MM:SS or YYYY-MM-DD HH:MM:SS and let you perform native time conversions on your data instead of mucking around with scripting on the server side.

You could supplement this field with a seperate timezone field if you need to perform timezone conversions.

Also beware of naming your field 'TIME' when that's a) likely to be a MySQL keyword and b) isn't really a helpful description of what that field contains. Perhaps 'end_time' or 'start_time' or 'interval_start' or something like that will be easier for someone else to interpret at a glance.

Keep in mind that 10:10 PM is later then 11:00 AM.

Really?
Computer thez nohhh...
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Aug 3, 2004, 07:08 PM
 
Heheh, yes really

Thanks for your help!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:06 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2