php - get record by date range in CodeIgniter -


i want search records between 2 dates in codeigniter have tried many methods not getting required result.

my model

function search($date1 , $date2 ){     $this->db->where('date<',$date1);     $this->db->where('date >',$date2);     $result = $this->db->get();     return $result; } 

my controller

function getsearch(){      $date1 = $this->input->post('txtdate1');   // 02-06-2015      $date2 = $this->input->post('txtdate2');   // 19-06-2015      $data['result']  = $this->result_model->search($date1,$date2);      $this->load->view("search_view",$data); } 

now want rows between 2 19 getting nothing. note: date type in mysql varchar

02-06-2015 suboptimal way store dates in sql, because don't collate sensibly. if can switch date or datetime data type, you'll able cool stuff indexing on date column. that's not asked.

also, wonder if have inequalities wrong? what's start , end of range of dates want search?

to answer specific question:

the date_format() mysql function convert char dates collatable dates. in particular,

  date_format('02-06-2015', '%d-%m-%y')  

gets date object dates.

so sort of php code work

$this->db->where("date_format(date, '%d-%m-%y') < ","date_format('$date1', '%d-%m-%y')"); $this->db->where("date_format(date, '%d-%m-%y') > ","date_format('$date2', '%d-%m-%y'):); 

if $date2 '01-06-2015' , $date1 '04-06-2015' give records fall on second , third days of june.

you may want instead [$date1,$date2] range.

$this->db->where("date_format(date, '%d-%m-%y') >= ","date_format('$date1', '%d-%m-%y')"); $this->db->where("date_format(date, '%d-%m-%y') <  ","date_format('$date2', '%d-%m-%y') + interval 1 day"); 

notice date column wrapped in function. defeats use of index table search on dates. if date column had date datatype, work. if had index on date mysql use highly efficient range scan.

$this->db->where("date_format(date, '%d-%m-%y') >= ","'$date1'"); $this->db->where("date_format(date, '%d-%m-%y') <  ","'$date2' + interval 1 day"); 

in raw sql come out to...

where date_format(date, '%d-%m-%y') >= '$date1'   , date_format(date, '%d-%m-%y') <  '$date2' + interval 1 day 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -