2

I have data in MySQL table like :

ID     dates                   value
1    2011-12-18 10:11:12      test1
2    2011-12-18 12:11:12      test2
3    2011-11-18 10:19:11      test3

When I tried to get value in module it not work, I write code like :

$query=$this->db->get_where('datas', array('dates' => date('Y-m-d')));

I need to get data from whole day like get data only from 2011-12-18.

Thanks

3 Answers 3

2

Try This:

$date="2018-09-19";
$this->db->select('*');
$this->db->from('tbl_name');
$this->db->where('DATE(date_time_col)', $date);
$query = $this->db->get();
$res= $query->result();

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

Comments

1

try with $this->db->like();

so it will be

$query=$this->db->like('dates', array('dates' => date('Y-m-d')));

the % will be added automatically

also you have datas as key, and should be dates based on database structure

Comments

0

You can do the following:

In your table, create an int field with the name ts.

When you insert data into this table, use the time() method to insert a timestamp into ts.

$data = array(
   'dates' => '<your date value>' ,
   'value' => '<your value>' ,
   'ts'    => time()
);

$this->db->insert('datas', $data); 

Then, when querying:

Use the mktime() method to create a timestamp range for the start & end of today.

$start = mktime() - date('h')*3600;    
$end   = mktime(); 

$this->db->where('ts >=', $start);
$this->db->where('ts <=', $end);

Make sure you've indexed the ts column. This will also be faster than the above LIKE method on larger datasets.

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.