Change Default Timezone in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
478 Views

By default CodeIgniter 4 application timezone is America/Chicago. 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 codeigniter 4 tutorial.

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

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.


How To Find Default Application Timezone?

Open App.php file from /app/Config folder.

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

public $appTimezone = 'America/Chicago';

When we work with the date and time functions, by default it will pick the application default timezone. Here, we have few more articles over Date & time class of CodeIgniter 4.


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 App.php
  • Using PHP function to set timezone

Set User Timezone by App.php

We can directly use App.php file to change the application timezone to user specific.

Let’s say we want to set the application timezone to India. Open App.php from /app/Config and update the value of appTimezone.

public $appTimezone = '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 BaseController.php file from /app/Controllers folder. BaseController is the default parent controller for all controllers in CodeIgniter 4.

//...
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
  //...
  
  date_default_timezone_set('Asia/Kolkata'); // Added user timezone
}

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


How To Get Application Default Timezone?

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

This function will returns whatever we have inside App.php file.

//...

echo app_timezone();

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

Find More on CodeIgniter 4 here

Leave a Comment