• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> MySQL >> How to Set Time Zone in PHP and MySQL

How to Set Time Zone in PHP and MySQL

November 27, 2010 by Lorensius Londa 2 Comments

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

ex:

–-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.

ex:

default-time-zone=’Asia/Jakarta’

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

ex:

mysql>SET GLOBAL time_zone = ‘+0:07’

3. To set timezone at runtime per connection (session), use SET time_zone=timezone

ex:

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;
Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Empty All Tables in a MySQL Database with PHP
  2. How To Define Global Absolute Path in PHP
  3. How to Enable MySQL InnoDB Storage Engine on XAMPP
  4. How to Create Facebook Application with PHP for Beginner

Filed Under: MySQL, PHP Tagged With: date, hosting, Mysql, PHP, server, timezone, vps

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. msignore says

    November 27, 2011 at 1:02 am

    thank you…

    Reply
  2. Holger Knust says

    February 2, 2012 at 11:02 pm

    I believe it should be SET GLOBAL time_zone = ‘+7:00′ for 7 hours difference — not 7 minutes as above.

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in