Failing to get daily sales between two dates after midnight - codeigniter 4
23:42 04 Jan 2023

Am failing to retrieve DAILY SALES in my codeigniter 4 pos system after mid night when query between two dates.

The POS system is working in the BAR/Club where their start time is 14:00 and end time is 03:00 (next day).

My current code only shows daily sales upto midnight. After mid night my opening hours change to new day returning nothing because no sales by then.

Here is what I tried

MODEL

//Get Daily Sales For Tenant ID - By Specific Opening/Closing time
    public function getdailySalesByTenant($t, $ot, $ct)
    {
        $builder = $this->db->table('orders as o');
        $builder->select('o.*, u.first_name as waiter');
        $builder->join('users as u', 'u.id = o.sold_by', 'left' );
        $builder->where('o.tenant_id', $t);
        $builder->where('o.created_at >=', $ot); 
        $builder->where('o.created_at <=', $ct);
        return $results = $builder->get()->getResult();
    }

CONTROLLER

//Daily Sales
    public function getdailySales()
    {
        $t = $this->settingsModel->where('user_id', $this->tenantId->tenant_id)->first();
        $ot = date('Y-m-d H:i:s', strtotime($t['opening_time']));
        $ct = date('Y-m-d H:i:s', strtotime($t['closing_time'].'+ 1 day'));
        
        $data = $this->transactionsModel->getdailySalesByTenant($this->tenantId->tenant_id, $ot, $ct);
        $response = [
                'success' => true,
                'data' => $data,
        ];
        return $this->response->setJSON($response);
    }

I want to record daily sales from 2023-01-05 14:00:00 to 2023-01-06 03:00:00

php datetime codeigniter codeigniter-4