tags:

views:

78

answers:

5

I have a bunch of entries in a database, which I want to sort via date, but the date format looks like this Y-m-d g:i:s, how could I search the database via the date, and not date time?

A: 

That's easy. Convert your dates into proper format first, which is Y-m-d h:i:s

Col. Shrapnel
It shows up in the database as Y-m-d h:i:s, but I am unsure on how I can go about querying the entries based on the date, and not the date time, like so: "SELECT * WHERE dateTime='2010-05-08'" for instance, is that a possibility?
@kielie if it's already in the proper format, use date() function for filtering: `WHERE date(dateTime)='2010-05-08'"` and there should be no problem with ordering already
Col. Shrapnel
A: 

You can do:

ORDER BY UNIX_TIMESTAMP(your_date)

your_date date needs to be converted first to Y-m-d h:i:s

fatnjazzy
poster does not mention timestamp usage in the table. Date is already Y-m-d h:i:s
Jakub
if it gets converted to Y-m-d h:i:s, no need to use timestamp for ordering. Y-m-d h:i:s will be ordered all right. That's the purpose of such a format
Col. Shrapnel
A: 

here is an example

$sql="SELECT cast(".$date." AS datetime) as sortdate FROM table order by sortdate ASC";

or

$sql="SELECT cast(DateColumnName AS datetime) as sortdate FROM table order by sortdate ASC";
JapanPro
+1  A: 
select * form table where date(datetime) = '2010-05-08'
ovais.tariq
+4  A: 

If it's a datetime field do this for ordering

ORDER BY date_field ASC

For searching do

WHERE DATE(date_field) = '2010-05-08'

If it's a varchar type field try looking at STR_TO_DATE (link)

Aaron W.