Change Default Timezone in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
11,829 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.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ 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.


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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5