Change Default Timezone in CakePHP 4 Tutorial

Reading Time: 4 minutes
2,976 Views

By default CakePHP 4 application timezone is UTC. If we want to change this timezone to something else like for any specific country. How can we do that?

Inside this article we will see the methods by the help of which we can change default timezone in cakephp 4 tutorial. Timezone is the process for date time chain worldwide.

Very simple steps to do and work with application timezone. This article will be very interesting to learn.

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.

.env Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command to make a copy of it.

Open project in terminal

$ cp .env.example .env

Next,

Load .env file in bootstrap.php file. By default the lines are commented. We need only to uncomment that.

Open bootstrap.php file from /config folder. Search for these and uncomment.

//...

if (!env('APP_NAME') && file_exists(CONFIG . '.env')) {
    $dotenv = new \josegonzalez\Dotenv\Loader([CONFIG . '.env']);
    $dotenv->parse()
        ->putenv()
        ->toEnv()
        ->toServer();
}

//...

Now, we able to use environment variables in application.

How To Find Default Application Timezone?

Open .env file from /config folder.

Inside this file, search for APP_DEFAULT_TIMEZONE, you will see the application default timezone.

export APP_DEFAULT_TIMEZONE="UTC"

When we work with the date and time functions, by default it will pick the application default timezone.

Note*: Internally .env variable is linked with app.php, Once we load then we can use it.

Methods To Change Application Timezone

There are several ways to change the default application timezone but here we will see two methods.

  • Set User timezone to .env
  • Using PHP function to set timezone

Set User Timezone by .env

We can directly use .env file to change the application timezone to user specific. Let’s say we want to set the application timezone to India.

Open .env from /config folder and update the value of APP_DEFAULT_TIMEZONE.

export APP_DEFAULT_TIMEZONE="Asia/Kolkata"

This timezone settings will be global. It means for entire application the default timezone will be Asia/Kolkata

Using PHP function to set Timezone

We can use PHP function date_default_timezone_set() to set timezone. We can call this php function anywhere means at parent controller of application or to any specific controller.

Open AppController.php file from /src/Controller folder. AppController is the default parent controller for all controllers in CakePHP 4.

<?php

declare(strict_types=1);

namespace App\Controller;

use Cake\Controller\Controller;

class AppController extends Controller
{
    public function initialize(): void
    {
        parent::initialize();

        date_default_timezone_set('Asia/Kolkata'); // Added user timezone

        $this->loadComponent('RequestHandler');

        //...
    }
}

Also we can use this PHP function to any specific controller’s initialize method to set timezone.

How To Get Application Default Timezone?

In CakePHP 4, we have a helper function available to get the application default timezone.

This function will returns whatever we have inside .env file.

//...

echo getenv("APP_DEFAULT_TIMEZONE");

OR

echo env("APP_DEFAULT_TIMEZONE");

We hope this article helped you to learn Change Default Timezone 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.