How to Get Last 30 Days Data in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
627 Views

Inside this article we will see how to get last 30 days data in codeigniter 4. Article has very classified information about last 30 days 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.

Last 30 days data selection is very helpful when we are creating data reports of application.

This article gives you a detailed on Codeigniter 4 to get Last 30 days data. Here, Creating a basic example of CodeIgniter from which we will select data in last 30 days.

Learn More –

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.


.env Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.


Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Database Connection

Open .env file from project root.

Search for DATABASE. You should see the connection environment variables. Give your updated details of database connection string.

 
#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = codeigniter4_app
database.default.username = root
database.default.password = root
database.default.DBDriver = MySQLi
database.default.DBPrefix =
   

Now, database successfully connected with the 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 all data from this table of last 30 days in CodeIgniter 4.


Create Query for Data

Open any controller from your application.

Let’s add Last 30 days 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('name, email, phone_no');
        $usersTable->where("created_at >= DATE(NOW()) - INTERVAL 30 DAY");
        $query = $usersTable->get();

        //echo $this->db->getLastQuery();

        echo "<pre>";
        print_r($query->getResult());
    }
}

Query Concept

Make use of MySQL Last 30 Days Data Selection Concept.

Generated Query

SELECT `name`, `email`, `phone_no` FROM `users` WHERE `created_at` >= DATE(NOW()) - INTERVAL 30 DAY

When we run this code, we will get result as

We hope this article helped you to How to Get Last 30 Days Data in CodeIgniter 4 Tutorial in a very detailed way.

Buy Me a Coffee

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 Twitter and Facebook.

Find More on CodeIgniter 4 here

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4