Set timezone in PHP and MySQL

Set timezone in PHP and MySQL

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

Stefan Bogdanescu

Founder & Senior Architect

Synchronizing PHP and MySQL Timezones for Reliable Calculations

When building applications that rely on temporal logic, ensuring consistency between your application layer (PHP) and your database layer (MySQL) is non-negotiable. The scenario where date() in PHP generates a timestamp while NOW() in MySQL generates another often leads to subtle bugs in reporting, billing, or scheduling logic. If these two time sources drift apart, calculations like TIMESTAMPDIFF become unreliable because they are comparing apples to oranges rather than equivalent moments in time.

The core issue stems from the fact that PHP and MySQL maintain independent timezone contexts by default. Setting date_default_timezone_set('Europe/Paris') affects how the PHP CLI or web server processes timestamps, but it has zero impact on the database engine. Similarly, trying to alter the global timezone of a specific MySQL session via SET time_zone = 'Europe/Paris'; works within that session but does not propagate to your PHP application code running on the same machine. You cannot simply "sync" them at runtime without addressing both environments explicitly.

To achieve true synchronization, you must treat the database server and the application server as distinct entities requiring specific configuration. The most robust approach involves aligning the database server's global timezone with the desired offset, while ensuring the PHP environment acknowledges this setting.

Configuring MySQL Timezone

MySQL stores all DATETIME and TIMESTAMP columns in UTC internally. When you execute a query like NOW(), it returns the current time adjusted by the server's session timezone or the global configuration. To make this predictable for your application, you should configure the MySQL server to operate in UTC unless there is a specific business requirement for local storage.

You can set the default timezone globally by modifying the my.cnf (Linux) or my.ini (Windows) configuration file. Locate the [mysqld] section and add the following line:

[mysqld]
default-time-zone = '+00:00'

Alternatively, if your application logic specifically requires Paris time stored directly in the database columns, you can set it to Europe/Paris. However, note that MySQL 8.0+ requires a restart after changing this setting, whereas older versions might only require reloading privileges. After restarting the service, verify the change with:

SELECT @@global.time_zone;
-- Expected output: 'SYSTEM' (if using system clock) or '+00:00'/'Europe/Paris' depending on config

If you are using a managed hosting environment where you cannot edit my.cnf, you can set the timezone per session, but this is less ideal for stateful applications. You would need to ensure every connection executes SET time_zone = 'Europe/Paris'; immediately after connecting. This adds overhead and complexity, suggesting that server-level configuration is the preferred path.

Aligning PHP Timezone Settings

On the PHP side, you have two primary methods to enforce a timezone: using php.ini or setting it dynamically in code. The dynamic approach offers more flexibility but requires careful placement.

If you are using Laravel, you should check your config/app.php file. Look for the timezone key and ensure it matches your target region:

'timezone' => 'Europe/Paris',

This setting is picked up automatically by the framework's date helpers. If you are writing vanilla PHP or using a different stack, you must call date_default_timezone_set('Europe/Paris'); at the very top of your entry script (e.g., index.php or public/index.php) before any other code executes. Placing this line inside a function or after loading configuration files is a common pitfall that results in UTC timestamps being generated inadvertently.

The Calculation Strategy

The most critical aspect of this integration is understanding how the calculations should happen. If your goal is to calculate the difference between a stored deadline and the current moment, you must ensure both sides agree on "now."

If MySQL is set to Europe/Paris and PHP is set to Europe/Paris, your query:

SELECT TIMESTAMPDIFF(HOUR, NOW(), finalize_at) FROM plans;

will return accurate hours relative to Paris time. However, if you prefer to store everything in UTC (the industry standard), you should force both environments to use UTC. In this case, NOW() returns UTC time, and your PHP date('Y-m-d H:i:s') function must also be configured for UTC (or the default system timezone is already UTC). Mixing a UTC database with a local-time PHP application will result in discrepancies that grow larger the further you are from UTC+0.

For complex applications, consider storing timestamps as DATETIME columns without timezone attributes and always treating them as UTC internally. Only convert to local time for display purposes. This decouples your data integrity from geographic location. If you are building a Laravel application, utilizing the built-in Carbon library simplifies this process significantly. You can create a helper method that automatically handles timezone conversion:

use Illuminate\Support\Facades\DB;

// Force UTC for database operations
DB::statement('SET time_zone = \'+00:00\'');

// Then calculate differences using Carbon which respects your configured app timezone
$diff = now()->diffForHumans($plan->finalize_at);

Conclusion on Implementation

There is no single magic variable in php.ini that magically updates the MySQL server's clock. The systems are independent. To solve this, you must explicitly configure the database server to use the desired timezone at the [mysqld] level and ensure the PHP application initializes with the matching timezone setting. The most maintainable architecture usually involves keeping the database in UTC and handling all local time conversions within the application logic using frameworks like Laravel or libraries like Carbon. This prevents ambiguity in data storage while allowing flexible presentation to users across different regions.

Our network