How to Get Month Wise Data in CodeIgniter 4 Tutorial

Inside this article we will see how to get month wise data in codeigniter 4. Article has very classified information about month wise data selection from database in codeigniter 4.

Selecting data rows from database in codeigniter 4, we have several options available as — Using Models, Using Query Builder, Using raw queries.

Monthwise data selection is very helpful when we are creating data reports of application.

This article gives you a detailed on Codeigniter 4 to get month wise data. Here, Creating a basic example of CodeIgniter from which we will sum number of users month wise.

Learn More -

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.

Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 -

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics — Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer -

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production // Do it to CI_ENVIRONMENT = development

Now application is in development mode.

Database Connectivity to Application

Open .env file from project root. Search for DATABASE. You should see the connection environment variables.

Let’s set the value for those to connect with database.

#-------------------------------------------------------------------- # DATABASE #-------------------------------------------------------------------- database.default.hostname = localhost database.default.database = codeigniter4_app database.default.username = root database.default.password = root database.default.DBDriver = MySQLi

Now, database successfully connected with application.

Sample Database Data Rows

Here, we have taken a sample database to understand this example. codeigniter4_app is the name of database.

Let’s say we have a users table inside it which contains users data. Please look into this screenshot to get clear vision about table.

Next,

We want to get the count of users month wise from this table in CodeIgniter 4.

Create Query for Data

Open any controller from your application.

Let’s add month wise concept into it to get data.

<?php namespace App\Controllers; class Site extends BaseController { private $db; public function __construct() { $this->db = db_connect(); } public function getUsers() { $usersTable = $this->db->table("users"); $usersTable->select('count(id) as total_users, Month(created_at) as month'); $usersTable->groupBy("Month(created_at)"); $query = $usersTable->get(); //echo $this->db->getLastQuery(); echo "<pre>"; print_r($query->getResult()); } }

Query Concept

Make use of Query grouping month wise to get total users count month wise.

Generated Query

SELECT count(id) as total_users, Month(created_at) as month FROM `users` GROUP BY Month(created_at)

When we run this code, we will get result as

We hope this article helped you to How to Get Month Wise Data in CodeIgniter 4 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on and .

Find More on CodeIgniter 4 here

Originally published at https://onlinewebtutorblog.com on October 13, 2021.

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors