Set your own timezone on a shared hosting

December 6, 2009 § 7 Comments

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

Tagged: , , , ,

§ 7 Responses to Set your own timezone on a shared hosting

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

What’s this?

You are currently reading Set your own timezone on a shared hosting at Rupom Here.

meta

%d bloggers like this: