How to Get Last 30 Days Data in CakePHP 4 Tutorial

Reading Time: 3 minutes
1,497 Views

Inside this article we will see how to get last 30 days data in cakephp 4. Article has very classified information about last 30 days data selection from database in cakephp 4.

Selecting data rows from database in cakephp 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.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

Create Database

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE mydatabase;

Successfully, we have created a database.

Database Connection

Open app_local.php file from /config folder. Search for Datasources. Go to default array of it.

You can add your connection details here to connect with your database. It will be like this –

//...

'Datasources' => [
        'default' => [
            'host' => 'localhost',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',

            'username' => 'root',
            'password' => 'sample@123',

            'database' => 'mydatabase',
            /*
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
             */
            //'schema' => 'myapp',

            /*
             * You can use a DSN string to set the entire configuration
             */
            'url' => env('DATABASE_URL', null),
        ],
  
     //...

//...

You can pass host, username, password and database.

Successfully, you are now connected with the database.

Sample Database Data Rows

Here, we have taken a sample database to understand this example.

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 CakePHP 4.

Create Query for Data

Open any controller from your application.

Let’s add last 30 days concept into it to get data.

<?php

declare(strict_types=1);

namespace App\Controller;

use Cake\Datasource\ConnectionManager;

class SiteController extends AppController
{
    private $db;

    public function initialize(): void
    {
        parent::initialize();

        $this->db = ConnectionManager::get("default");
        $this->autoRender = false;
    }

    public function getUsers()
    {
        $results = $this->db
            ->newQuery()
            ->select('name, email, phone_no')
            ->from('users')
            ->where('created_at >= DATE(NOW()) - INTERVAL 30 DAY')
            ->execute()
            ->fetchAll('assoc');

        print_r($results);
    }
}

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 of last 30 days.

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

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.