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
https://forums.phpfreaks.com/topic/251015-order-by-day/
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
https://forums.phpfreaks.com/topic/251015-order-by-day/#findComment-1287696
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
https://forums.phpfreaks.com/topic/251015-order-by-day/#findComment-1287712
Share on other sites

Use either one of the following for a 'custom' sort order -

 

ORDER BY FIELD(day,'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),end_time

 

or

 

ORDER BY FIND_IN_SET(day,'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'),end_time

Link to comment
https://forums.phpfreaks.com/topic/251015-order-by-day/#findComment-1287802
Share on other sites

Thanks for all of your comments guys,

 

 

Each method is good in solving my problem although PFMaBiSmAD's suited my plan best.

 

 

The events repeat on the same day of every week, so a timestamp would not work for simply saying Monday.. or would it?

 

 

George.

Link to comment
https://forums.phpfreaks.com/topic/251015-order-by-day/#findComment-1287834
Share on other sites

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.