0

I am trying to order a database by date but I am having trouble sorting it. The database is created in SQL through PHP and the column I want to use to sort by adds the date data in this format:

2011-10-26 07:10

The above date is also a 'varchar'. EDIT: changed to 'datetime' thanks to suggestions

Any idea so that I can sort the rows in the table using the date column?

I made a separate script to narrow this problem down:

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("thoughtsdata",$con);


$order = "SELECT * 
FROM  `thoughts` 
ORDER BY  `thoughts`.`DateTime` ASC 
LIMIT 0 , 30";

if(!mysql_query($order,$con))
{
die('Could not order database: ' . mysql_error());
}


mysql_close($con);
?>

I now get no errors when running this script in my browser but my table remains unsorted:

  Edit    Inline Edit     Copy   Delete Hey sdfasdf    2011-10-26 19:41:00
  Edit    Inline Edit     Copy   Delete Hey asdfasdf    2011-10-26 07:47:00 
  Edit    Inline Edit     Copy   Delete Hey dfasdfasdf    2011-10-26 19:47:00   
  Edit    Inline Edit     Copy   Delete aaa how to sort this    2011-10-26 07:54:00 
  Edit    Inline Edit     Copy   Delete aaa any progress!!??    2011-10-26 08:13:00 
  Edit    Inline Edit     Copy   Delete oh no!  grrr    2011-10-26 08:21:00 
  Edit    Inline Edit     Copy   Delete zzTOp   thinking about sorting this out!    2011-10-27 19:12:00

What seems to me happening, is that the database is sorted but it does not stay like that.

12
  • wow, thanks for the quick response. I was able to change the datatype easily in php to datetime...now I am having trouble sorting the table from the php script. I have this line in php: mysql_query('SELECT * FROM thoughts', 'ORDER BY DateTime'); Commented Oct 26, 2011 at 17:57
  • perhaps is did not execute the statement correctly Commented Oct 26, 2011 at 17:58
  • The correct syntax would be: mysql_query("SELECT * FROM thoughts ORDER BY fieldname"); Commented Oct 26, 2011 at 18:54
  • DateTime is the column name....is that the same as the fieldname? Commented Oct 26, 2011 at 19:17
  • Add any output from the script (including all errors) to your post. If there is no output, check your web server error logs to find out what happened. Commented Oct 26, 2011 at 19:21

3 Answers 3

1

You could try using an order clause like this:

ORDER BY UNIX_TIMESTAMP(fieldname) ASC

However, if your field is in YYYY-MM-DD hh:mm format like it looks, an alpha sort should work correctly. This is all under the assumption that you are working with an existing system and can't / aren't allowed to change the field's datatype.

Sign up to request clarification or add additional context in comments.

3 Comments

I can change the fields data type, but I am having trouble sorting hte column from the php script I made. Any suggestion?
If you're actually sorting it in the script itself, look at PHP's usort() function: us3.php.net/manual/en/function.usort.php
hmm, I want to sort the data in the SQL table but from the PHP script. Here is what I have tried to execute: mysql_select_db("thoughtsdata",$con); $order = mysql_query("SELECT * FROM thoughts ORDER BY DateTime"); if(!$order,$con) { die('Could not order database: ' . mysql_error()); }
1

Better way to convert it, for ex. with php, to "normal" datetime/timestamp and than use sort.

Main reason to do it is it's far more faster than using sorting by string.

Comments

0

You can CAST or convert the varchar to a real datetime, that is what it should of been since it is really a datetime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.