How to Set Time Zone in PHP and MySQL

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

ex: date_default_timezone_set(“Asia/Jakarta”);

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=Asia/Jakarta or

–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


mysql>SET time_zone=’Asia/Jakarta’

To check the global and session timezone:

mysql>SELECT @@global.time_zone, @@session.time_zone

select @@global.time_zone, @@session.time_zone;
facebooktwittergoogle_plusredditpinterestlinkedinmailby feather


Leave a Reply

Your email address will not be published. Required fields are marked *