Cannot Set date.timezone In php.ini file

Cannot Set date.timezone In php.ini file

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Cannot Set date.timezone In php.ini File

When running PHP scripts that utilize date functions, you might encounter a persistent warning: Warning: It is not safe to rely on the system's timezone settings. This message appears when the date.timezone configuration directive is missing or invalid. While the error seems straightforward—PHP requires an explicit timezone—it often leads developers down a rabbit hole of checking file permissions, restarting services, and inspecting additional .ini files without success.

The root cause frequently lies in how PHP's configuration loading hierarchy works, specifically regarding Apache modules. In environments like Debian or Ubuntu, where the source example points to /etc/php5/apache2/php.ini, simply adding date.timezone = Asia/Jakarta to that file and restarting Apache often fails if Apache is not reading that specific file at all.

The Configuration Loading Hierarchy

PHP loads configuration from multiple sources in a specific order. Directives defined later override those earlier, but only if they are actually loaded. For Apache, the primary configuration file is indeed php.ini, but there is a critical prerequisite: the Apache module must be configured to load it.

If you are using PHP 5.x or older versions, you often see references to /etc/php5/apache2/php.ini. However, Apache might be loading a different php.ini located in the root directory (/etc/php5/apache2/) or relying entirely on .ini files dropped into /etc/php5/apache2/conf.d/.

To verify which configuration is actually active, you cannot rely solely on checking the file you edited. Instead, run the following command within your PHP script:

echo ini_get('date.timezone');

If this returns an empty string or UTC, the directive is not taking effect. This usually means Apache is loading a different configuration file than the one you modified. In many Linux distributions, the master php.ini in /etc/php5/apache2/ might be a symbolic link or a template that doesn't get re-read after an edit unless explicitly told to do so by the Apache module directive.

Verifying the Loaded Configuration

The most reliable way to identify which configuration file is active is via the command line or a script:

php -i | grep "Loaded Configuration File"

Or, using phpinfo():

<?php phpinfo(); ?>

Look for the section titled "Configuration File (php.ini) Path". If this path does not match /etc/php5/apache2/php.ini, your edits are being ignored. You might find that Apache is loading a file from /usr/lib/php/ or relying on conf.d directories where you haven't placed your custom settings correctly.

The Role of conf.d and Overrides

In modern PHP stacks, it is common to place specific directives in files within /etc/php5/apache2/conf.d/. If your main php.ini file contains a commented-out line like ;date.timezone =, or if the main file is empty, you must ensure the directive exists in an active include path.

If you add date.timezone = Asia/Jakarta to the main php.ini but still face issues, check for conflicting directives elsewhere. Sometimes, a user-level .ini file in /etc/php5/apache2/conf.d/ might be overriding your settings with an empty value or an incorrect timezone identifier. Ensure that no other .ini file contains date.timezone = UTC or leaves it unset.

Restarting the Service Correctly

Once you have confirmed that the configuration is being loaded and the directive is correctly set to a valid timezone (like Asia/Jakarta), a service restart is mandatory. The command sudo /etc/init.d/apache2 restart is generally correct for Debian-based systems, but ensure that Apache has reloaded its modules.

If you are using a package manager like apt, consider updating the configuration file path references to match your system's actual PHP version (e.g., /etc/php/5.6/apache2/php.ini). Relying on hardcoded paths from old tutorials can lead to confusion when upgrading packages.

Why This Happens in Production

The warning about relying on system timezone settings is not just noise; it forces developers to be explicit. If you leave date.timezone unset, PHP defaults to UTC. When you convert UTC timestamps to local time without setting the timezone, your output will be off by several hours depending on your server's location.

In Laravel applications or other frameworks, this can cause massive discrepancies in logged events versus user-facing times. Always ensure that date.timezone is set in the production environment's active configuration. If you are using a framework like Laravel, you might also want to verify the timezone in the .env file if applicable, though that overrides the php.ini setting only for specific functions depending on the version.

For more robust configuration management, consider moving away from editing the main php.ini directly. Instead, create an override file in conf.d/, name it 20-date.ini, and place your settings there. This ensures that if you upgrade PHP or switch between Apache modules (like switching to FPM), your timezone settings persist without conflict.

Our network