Welcome Guest, Not a member yet? Register   Sign In
Easiest way to sort by date...
#1

[eluser]codelearn[/eluser]
Quick question:

What is the easiest way to return data from a MySQL table and sort it by date.. so if you had a date field (in 2007-12-14 format) what would be the easiest way to say return all the values that were in January?

Thanks.
#2

[eluser]Armchair Samurai[/eluser]
I'd do it like this
Code:
SELECT foo
FROM bar
WHERE created >= '2008-01'
AND created < '2008-02'
ORDER BY created ASC
#3

[eluser]Developer13[/eluser]
If >= 2007-01-01 AND < 2007-02-01 doesn't work,then you could always use MySQL's SUBSTR() function:

Something like this should work:

SELECT * FROM table WHERE SUBSTR(date_field, 0, 7) = '2007-01';

See the following for more information:
http://dev.mysql.com/doc/refman/5.0/en/s...ion_substr
#4

[eluser]codelearn[/eluser]
Code:
$this->db->where('SUBSTR(date,1,7)',$_POST['date_sort']);


Excellent as always, thanks guys.




Theme © iAndrew 2016 - Forum software by © MyBB