Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings
When upgrading PHP versions, specifically moving to 5.5.7 or later, you will inevitably encounter a strict warning regarding timezone configuration: Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. This message indicates that the runtime environment has stopped trusting the system default and requires an explicit declaration of your timezone identifier. If you have already configured date.timezone in your php.ini, yet the warning persists or phpinfo() shows a different value (like UTC), the issue almost certainly lies in how PHP resolves configuration files, not in the content of the file itself.
The core problem is that modern PHP distributions often support multiple configuration sources with a specific precedence order. You may have edited /usr/local/php5.5.7/lib or a system-wide php.ini, but your application might be loading a different configuration file entirely. In many Linux environments, particularly those using FastCGI processes like PHP-FPM or Apache with mod_php, the runtime environment often defaults to --ini-file="". This means PHP does not automatically look for php.ini in the current directory or the system path; it relies on a specific entry point defined by the web server or the process manager.
To resolve the conflict between your phpinfo() output and your manual edits, you must identify the active configuration file. Run the following command in your terminal:
php -i | grep "Configuration File"
This command explicitly prints the path to the currently loaded php.ini (or the equivalent .conf file for PHP-FPM). If this line is empty, PHP is using default values provided by the SAPI (Server API), which explains why your manual edits in /usr/local/php5.5.7/lib are being ignored.
Once you know the active configuration path, you have two viable strategies to fix the warning. The first is the "global" approach: locate the exact file printed by php -i and add the following directive at the very top of that file (before any [Date] sections):
date.timezone = "Europe/Paris"
Placing this line early ensures it overrides default SAPI settings. After saving the file, you must restart your web server or PHP-FPM service for the changes to take effect. If you are running a custom-installed PHP (like /usr/local/php5.5.7), ensure your web server is actually invoking that specific binary and not falling back to a system default located at /usr/bin/php.
The second strategy is the "local" approach, which is often more robust for Symfony projects. Instead of relying on global php.ini settings, you can set the timezone directly within your application's bootstrap process. Create or edit the file where your application starts up (commonly app/config/bootstrap.php in older Symfony 2 structures or a dedicated initializer script). Add this line:
date_default_timezone_set('Europe/Paris');
This method is generally preferred for Symfony applications because it decouples your application's time assumptions from the server's OS configuration. It ensures that regardless of how the server is deployedâwhether on a shared host, a Docker container with stripped-down configurations, or a specific PHP buildâthe application consistently operates in Europe/Paris.
Be aware that while date_default_timezone_set() works, it can be slightly slower than setting the value in php.ini because it incurs a function call overhead on every request initialization. However, the performance difference is negligible for most applications and pales in comparison to the reliability of explicit configuration. The warning appears specifically because PHP 5.5+ treats an unset timezone as UTC but logs a warning, which can clutter production logs and obscure real errors.
If you are managing multiple environments (development vs. production), ensure your CI/CD pipeline or deployment scripts handle this setting. For instance, if you use Composer to manage dependencies, some older packages might assume a specific timezone. Explicitly setting it in bootstrap.php guarantees consistency across all environments without requiring changes to the underlying OS or global PHP configuration files.
Finally, verify that your Symfony application is correctly handling date objects. Even with the timezone set, ensure that your database stores dates in UTC but formats them according to the user's local timezone (in this case, Europe/Paris) only when displaying data to the end-user. This separation of concernsâstorage in UTC, display in local timeâis a best practice that prevents daylight saving time confusion and simplifies internationalization efforts.
