Welcome Guest, Not a member yet? Register   Sign In
How to retrieve hours for specific dates in CodeIgniter?

I'm facing a challenge with structuring a model query in CodeIgniter 3's active record. My goal is to retrieve the sum of hours within a specific range of dates (individual months of a year). In traditional MySQL, I can achieve this using a query like:

SELECT SUM(table_time) AS total FROM table_name WHERE table_date >= '2021-05-01' AND table_date <= '2021-05-31';
However, I'm struggling to replicate this functionality using active record in CodeIgniter. Here's what I've attempted so far:

$this->db->select("SUM(hours) AS total");
$this->db->where('data', '2021-05-01 >= 2021-05-31');

$query1 = $this->db->get();
if ($query1->num_rows() > 0) {
  $res = $query1->row_array();
  return $res['total'];
return 0.00;
Unfortunately, with $this->db->where('data', '2021-05-01 >= 2021-05-31');, I'm not getting any results. I'm wondering how I can solve this issue. Essentially, I'm struggling to instruct the active record query to extract hours for dates within the entire month. I would greatly appreciate any suggestions or guidance.

(This post was last modified: 06-16-2023, 08:52 AM by JustJohnQ.)

When is 2021-05-01 >= 2021-05-31??
You should use 2 where:
First one ‘data >= 2023-05-01’
Second one ‘data <= 2023-05-31’

PHP Code:
$this->db->where('table_data >=''2021-05-01');
$this->db->where('table_data <=''2023-05-31'); 

Theme © iAndrew 2016 - Forum software by © MyBB