Shared Hosting Timezone & DST Solution – PHP MySql

I was having trouble in solving timezone and DST (daylight saving) issue in my PHP / MySql solution shared hosting. Here’s what I found which really made it easy for me for not to get worried afterwards for this issue.

Shared Hosting Timezone & DST Solution

You just need to add the following few lines in your config or index file, or right after the code where you get connected to the database:

Note, I have used timezone for Australia/Melbourne and it is GMT +10. You can change the ones you want for specific countries/cities by going through the function details of date_default_timezone_set and finding out their GMT on google.

$country_city = “Australia/Melbourne”;
$time_zone = 10;
$dst = intval(date(‘I’)); // this will be 1 in DST or else 0
$time_zone += 1;

mysql_query(“SET time_zone = ‘+”.$time_zone.”:00′”);

Make sure you set the $country_city and $time_zone variables as the default timezone of your wanted country/city, and $dst variable will further change it by checking if its DST there or not.

The above mentioned code will resolve the timezone as well DST issues you’re facing for both, PHP as well as MySql. For MySql, you need to make sure that you call this code or function on the places where you’re connecting to the database.

Hope it solves the problem.

If any of you have any other alternate or better solution, please share on comments section below.

Share with:

Leave a Reply

Login with:

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