The Central Application Configuration File
In a CodeIgniter application, you need to centralize configuration settings so that environment-specific values are managed consistently. If your goal is to avoid modifying php.ini or .htaccess, the primary location for this logic is within the framework's core configuration file: application/config/config.php. This file acts as the single source of truth for global application settings, making it the standard approach for frameworks like CodeIgniter 3 and 4.
To set the default time zone globally, you simply add a call to date_default_timezone_set() at the very top of this file. This ensures that every subsequent date(), datetime, or timezone-related function within your application inherits the configured offset immediately.
// application/config/config.php
$config['base_url'] = '';
// Set the default time zone here
date_default_timezone_set('Asia/Kolkata');
By placing this line in config.php, you eliminate the need to prefix every entry point or controller with a manual timezone setter. This approach leverages PHP's behavior where date_default_timezone_set() can only be called once per request, and doing so early ensures that all subsequent calls to date functions reflect the intended offset without ambiguity.
Handling Database Time Zones
While setting the PHP time zone is straightforward, MySQL requires separate attention if your application stores timestamps in UTC but needs to display local times. The database server's own time zone setting is independent of the PHP runtime setting. If you cannot alter php.ini or .htaccess, you can configure the MySQL connection to handle timezone conversion automatically at the protocol level.
In modern PHP applications, including those built on CodeIgniter, the best practice is to store all timestamps in UTC within the database and convert them only for display. To ensure the database driver handles this correctly, you must set the date_default_timezone_set in PHP as shown above, but you also need to configure the MySQL connection.
If you are using a database abstraction layer or a raw connection, you can execute a query on every connection to align the session time zone:
SET time_zone = '+05:30';
However, relying solely on the database server's time_zone variable is often discouraged because it does not guarantee that PHP functions will behave as expected if they are called before the DB connection is established. The most robust solution remains setting the PHP default timezone in config.php, which aligns with how CodeIgniter initializes its environment.
Framework-Specific Considerations
CodeIgniter 4 introduced a more modular configuration structure compared to CodeIgniter 3. In CI4, you might find yourself configuring the time zone in the App\Config\Database class or within the service provider if you are injecting dependencies manually. However, for general utility functions like date() that do not depend on database objects, the global config file remains the correct spot.
It is important to note that some frameworks provide helper functions that abstract this away entirely. If you are migrating from older versions or using third-party libraries, always check if a helper function exists that wraps the timezone logic. For native PHP usage within CodeIgniter, however, explicit configuration is preferred over implicit assumptions.
Common Pitfalls and Best Practices
One common mistake developers make is attempting to set the time zone inside a controller or model method. Since date_default_timezone_set() can only be executed once per request, placing it later in the code flow will have no effect if it was already called elsewhere (or if PHP reports that the timezone is already set). Similarly, relying on system-level defaults without explicit configuration leads to inconsistent behavior across different servers and environments.
Another subtle issue arises when mixing UTC storage with local display logic. If your database stores DATETIME fields as 2023-10-27 14:30:00, and you want this to appear as 18:30:00 in Asia/Kolkata, you must ensure that PHP's internal clock matches the timezone you intend for display. If the server runs in a different timezone (e.g., UTC), your raw date output will be off by hours unless explicitly corrected.
For production environments where consistency is critical, avoid relying on php.ini changes if you want to maintain control over application-specific logic. Instead, keep the configuration within the CodeIgniter project structure using config.php. This ensures that deployment scripts can easily override or adjust time zones without touching server-wide configurations.
If you require additional guidance on managing complex multi-timezone applications or need a framework-agnostic solution for date handling, resources like https://laravelcompany.com offer insights into modern PHP practices that often apply broadly to other frameworks as well.
