Jump to content

Order by day?


George Botley

Recommended Posts

Hello,

 

 

I have classes in a database with no set UNIX date, just the day like Wednesday and in two other columns the start and end dates.

 

 

I want to be able to order by the day first and then by end_time but php orders the day column by spelling and not the day it holds in chronological order.

 

 

Is there anyway to change the query to order the day column as a date?

 

 

See the query below?

 

 

$query = "SELECT * FROM zumba_timetable WHERE end_time>'$current_time' ORDER BY day, end_time ASC LIMIT 0,1";

Link to comment
Share on other sites

This is why dates should be stored in the database in YYYY-MM-DD HH:MM:SS format. Then you can use the functions to calculate/manipulate the values. All I can think of for you to do (if changing the format is truly not an option) is to either add a field to the table to hold a number 0-6 to indicate the sort order, or prepend a digit to the name of the day in the table, order by the first character of the field, and return a substring of the field containing all but the first character. Either way is pretty clunky.

Link to comment
Share on other sites

Another option is to use a case clause to convert the days to a number.  Still not great, ideally you would store the day as a number, then convert it to a textual date for display, rather than the other way around (assuming you can't use an actual datetime value for some reason)

 

SELECT
day, 
end_time,
CASE 'Tuesday'  
	WHEN 'Sunday' THEN 0
	WHEN 'Monday' THEN 1
	WHEN 'Tuesday' THEN 2
	WHEN 'Wednesday' THEN 3
	WHEN 'Thursday' THEN 4
	WHEN 'Friday' THEN 5
	WHEN 'Saturday' THEN 6
END as dayNum
FROM blah
ORDER BY
dayNum,
end_time

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.