Codeigniter: Order by ascending date

孤街浪徒 提交于 2019-12-22 14:01:08

问题


Hi this is my model code for ordering and getting the data:

$this->db->order_by($oBy, "asc");
$query = $this->db->get('books');

Everything is working fine, however in my database i am storing the date as a string, e.g. 01-Jan-2014.

Therefore when i order the date it will order it by the day and not year, may i know how can i solve it by sorting by the year, however the data will still display out as 01-Jan-2014 and also it will be displayed in ascending order? Thank you!

Error:


回答1:


$this->db->select('str_to_date('.$oBy.', "%d-%b-%Y") day',false);//select your colum as new column name wich is converted as str ot date
//yo can do select more.
$this->db->order_by('day','ASC');
$query = $this->db->get('books');

This will solve your problem




回答2:


I suppose that $oBy is your column name

$this->db->order_by("str_to_date(" . $oBy . ", '%d-%b-%Y')", "asc");

%d = Day of the month, numeric (00..31)

%b = Abbreviated month name (Jan..Dec)

%Y = Year, numeric, four digits



来源:https://stackoverflow.com/questions/28381738/codeigniter-order-by-ascending-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!