How to Get Last 2 Weeks Data in CakePHP 4 Tutorial

Reading Time: 4 minutes

Inside this article we will see how to get last 2 weeks data in CakePHP 4. Article has very classified information about last week 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 2 weeks data selection is very helpful when we are creating data reports of application.

This article gives you a detailed on CakePHP 4 to get Last 2 weeks data. Here, Creating a basic example of CakePHP from which we will select data in last 2 weeks.

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.


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.


We want to get all data from this table of last 2 weeks in CakePHP 4.

Create Query for Data

Open any controller from your application.

Let’s add last 2 weeks concept into it to get data.



namespace App\Controller;

use Cake\Datasource\ConnectionManager;

class SiteController extends AppController
    private $db;

    public function initialize(): void

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

    public function getUsers()
        $results = $this->db
            ->select('name, email, phone_no')
            ->where('created_at BETWEEN CURDATE()-INTERVAL 2 WEEK AND CURDATE()')


Query Concept

Make use of MySQL Last 2 Weeks Data Selection Concept.

Generated Query

SELECT `name`, `email`, `phone_no` FROM `users` WHERE `created_at` BETWEEN CURDATE()-INTERVAL 2 WEEK AND CURDATE()

When we run this code, we will get result as expected for last two weeks.

We hope this article helped you to learn How to Get Last 2 Weeks 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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more