Set your own timezone on a shared hosting

If you are a web application developer and had opportunities to work on various client servers around the globe, you may have already experienced the timezone related problems in the server script. This problem comes when the hosting is a shared one and the website runs for a different timezone than the server’s timezone. For example – I am running a website for my local community here in Dhaka/Bangladesh (timezone GMT + 7.00) but my server which is a shared one resides in the United States (say a timezone like GMT – 7). As this is a shared server, the timezone was set by the administrator to their local time. So every call to PHP date() and/or time() function will always return the server’s local time causing a mismatch with my local time in my website.

To overcome this situation, you have to change your default date time using PHP function date_default_timezone_set(your_time_zone_string). For my case, it was following:

date_default_timezone_set('Asia/Dhaka');

Add this line before executing/calling any date and/or time functions.

Below might be a quick test for you:

—————————
<?php
echo 'OLD Time: ' . date('Y-m-d H:i:s a');

date_default_timezone_set('Asia/Dhaka');

echo '<br />';

echo 'New Time: ' . date('Y-m-d H:i:s a');
?>
—————————-

That’s it.

Read more on function date_default_timezone_set: http://us3.php.net/date_default_timezone_set

See the list of supported timezones here: http://us3.php.net/manual/en/timezones.php

Happy PHPing!

Advertisements

7 thoughts on “Set your own timezone on a shared hosting

    1. There are some ways to handle timezone in MySQL. But I would suggest you to avoid depending on MySQL’s default date/time behavior in this case. Instead, prepare necessary date/time inputs in PHP and then push your insert/update queries. This is easier to handle…and you only require to synchronize your timezone in server script.

      Thanks.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.