Sometimes when working with PHP-MySQL application, we have to explicitly set the time zone in our application in order to match the client’s time zone. Since PHP 5.3, time zone setting is mandatory and have to be set explicitly in application or in PHP configuration file (php.ini, for global used) or you will get PHP warning message ‘ It is not safe to rely on the system’s timezone settings. bla bla bla‘. I faced this problem when moved my application from my old server to a new VPS server located overseas with different time zone. Since my old server running PHP 5.1 and located on the same time zone with my clients, i had no problem with time zone before.
So there are two ways to set time zone in PHP
1. For global scope, add date.timezone parameter in php.ini file
ex: date.timezone = Asia/Jakarta
Note: you need to restart web server to apply the changes
2. For local application scope, add date_default_timezone_set() function in PHP script
Just like in PHP, we can also set time zone on MySQL server that differs with system’s default time zone . It can be done at runtime or at startup. Note that by default MySQL uses system/server time zone.
1. To set time zone at startup, append –timezone=timezonename option to mysqld_safe
–timezone=+00:07 ( note ‘+0:07’ indicates the offset from GMT)
or from MySQL configuration file (my.cnf), add default-time-zone=’timezone’ at [myqld] section.
Using this way, the settings will be applied to all MySQL connections since startup.
2. To set time zone at runtime and applied to all connections, use SET GLOBAL time_zone = timezone
mysql>SET GLOBAL time_zone = ‘+0:07’
3. To set timezone at runtime per connection (session), use SET time_zone=timezone
To check the global and session timezone:
mysql>SELECT @@global.time_zone, @@session.time_zoneby
Holger Knust says
I believe it should be SET GLOBAL time_zone = ‘+7:00′ for 7 hours difference — not 7 minutes as above.