Managing time and date-related operations is an important component of web development since it ensures accurate data representation and functionality. Setting the default timezone in CodeIgniter 4 is critical to ensuring that date and time functions work consistently across your application. You may verify that timestamps and time-based calculations are accurate and synchronised by selecting the default timezone.
By default in CodeIgniter 4 application timezone is UTC.
We will walk you through the process of setting the default timezone in CodeIgniter 4 in this article. We’ll look at how to use configuration files to set the default timezone, ensuring that your application’s date and time operations are in sync with the intended timezone.
Read More: CodeIgniter 4 How To Get Database Name Example 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 = 'UTC';
When you work with the date and time functions, by default it will pick the application’s default timezone.
Here, you can find 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 you will see two methods.
- Set User timezone from App.php
- Using PHP function to set timezone
Method #1: Set User Timezone from App.php
You can directly use App.php file to change the application timezone to user specific.
Let’s say you want to set the application timezone to India.
Open App.php from /app/Config folder 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
Read More: CodeIgniter 4 Call Spark Command From Closure Routes
Method #2: Using PHP function to set Timezone
You can use PHP function date_default_timezone_set() to set timezone. You 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 you can use this PHP function to any specific controller’s constructor method to set timezone.
How to get Application default Timezone?
In CodeIgniter 4, a helper function available to get the application default timezone.
This function will returns whatever you have inside App.php file.
//...
echo app_timezone();
That’s it.
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.