Jump to content

Using STR_TO_DATE to sort.


pwnuspoints

Recommended Posts

Hey phpfreaks!

 

As always; your help is appriciated- Thanks in advance!

 

I have several dates housed as text strings in a table. Each date is written in date("F j, Y") format [e.g. "January 28, 2009"].

When I display those dates on a php page, it has a tendancy to order them alphabetically-- which I find confusing. So, I'm trying to use the STR_TO_DATE sql function to display these strings in by date; it's not working.

 

My query looks like this:

 

$result = mysql_query("SELECT * FROM table WHERE display LIKE '1' ORDER BY STR_TO_DATE('createdate', 'F j, Y') DESC ");

 

What am I doing wrong?

 

Thanks Again.

Link to comment
https://forums.phpfreaks.com/topic/142826-using-str_to_date-to-sort/
Share on other sites

My query now reads as follows:

 

$result = mysql_query("SELECT * FROM table WHERE display LIKE '1' ORDER BY STR_TO_DATE(createdate, '%F %j, %Y') ASC ");

 

>.> it appears to be ordering them by my primary key instead of by date. This is so frustrating.

 

Is what I'm trying to accomplish even possible?

'%F %j, %Y' is not correct. Based on reading the page in the mysql manual, it should be '%M %e, %Y' or '%M %d, %Y' depending on if your days have leading zeros.

 

Doing your query this way is exceedingly slow and the current format you are using takes several times the amount of storage that using a DATE data type would. You should convert to use a DATE data type.

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.