Table of Contents
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 –
- How to Get Last 2 Weeks Data in CodeIgniter 4 Tutorial
- How to Get Last 30 Days Data in CodeIgniter 4 Tutorial
- How to Get Last 60 Days Data in CodeIgniter 4 Tutorial
- How to Get Last 7 Days Data in CodeIgniter 4 Tutorial
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.
Environment (.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.
- Compare Dates and Times in CodeIgniter 4 Tutorial
- Concept of Date And Time in CodeIgniter 4 Tutorial
- CodeIgniter 4 Getter And Setter Time Class Methods
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.
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.