0

I have the following queries in codeigniter to find any data in which the datetime field is more than 24 hours. Here is my try...

$this->db->where('date_reg >', 'DATE_SUB(NOW(), INTERVAL 1 DAY)');

But the above query is not working!

What I want in short is, return all rows which the date_reg is more than 24 hours...

I can not use query in codeigniter! Here is the format for date_reg field....2019-02-19 08:00:00

How do i do that?

G_real
  • 1,137
  • 1
  • 18
  • 28
MR.Internet
  • 547
  • 4
  • 19

3 Answers3

3

Are you looking for an older record that was recorded within 24 hours of the current date? Because let's take today's date. 2019-02-21.

$this->db->where('date_reg >', 'DATE_SUB(NOW(), INTERVAL 1 DAY)');

This query is essentially saying Find records greater than the current date. So it will look for records between 2019-02-21 and 2019-02-22, which might return empty.

Try this instead. I try to analyze the question as much as possible, rather than throw an half-assed answer

$this->db->where('date_reg <=', 'DATE_SUB(NOW(), INTERVAL 1 DAY)');
Kebab Programmer
  • 1,213
  • 2
  • 21
  • 36
1

Try this:

$this->db->select('*');
$this->db->from('your_table');
$this->db->where('date_reg >=', '(NOW() + INTERVAL 1 DAY)', false);
$this->db->get();
  • not working, the issue might be my date_reg field type is datetime, is that might be the problem? – MR.Internet Feb 21 '19 at 11:38
  • I have tested it. SELECT * FROM `customer_entity` WHERE created_at >= (NOW() + INTERVAL 1 DAY) Its working on my side for my customer column. – Abdulrehman Sheikh Feb 21 '19 at 11:38
  • I also have datetime thats not a problem you can do one more thing let me share – Abdulrehman Sheikh Feb 21 '19 at 11:39
  • $this->db->where('DATE(date_reg)', '>=', '(NOW() + INTERVAL 1 DAY)'); – Abdulrehman Sheikh Feb 21 '19 at 11:40
  • apply DATE() function on your field which will compare date .. try it if it works then will check for hour as well – Abdulrehman Sheikh Feb 21 '19 at 11:40
  • 1
    this isn't working at all ... the first parameter is the key the second the value and the third is a bool whether you want to escape it or not (default is `true`) - take a look here https://www.codeigniter.com/user_guide/database/query_builder.html?highlight=where#CI_DB_query_builder::where a proper solution would be `$this->db->where('date_reg >=', '(NOW() + INTERVAL 1 DAY)', false);` – Atural Feb 21 '19 at 12:04
0

Try this;

$this->db->query('SELECT * FROM your_table WHERE date_reg >= now() + INTERVAL 1 DAY;');
Gündoğdu Yakıcı
  • 677
  • 2
  • 10
  • 31