Running web scripts in your Timezone

From DreamHost
Jump to: navigation, search

Many web programs log or show the time. If these programs use the local time, they'll be in the US West Coast Timezone that DreamHost uses for its servers.

However, it is easy to change that for your scripts.

Look for the .htaccess in your domain's directory (usually at $HOME/your.domain.name). If there isn't any, just create a new one (it's a plain text file).

Now, you have to know the name of your local timezone. This is usually in the form Continent/City (or Continent/Country/City). If you have a shell account, look for the system file /usr/share/zoneinfo/zone.tab, and find your City there. If your City isn't here, you should look for the largest City in your timezone.

For instance, if you live in or near Buenos Aires, Argentina (in South America), your timezone is found in a line like this:

AR  -3436-05827 America/Argentina/Buenos_Aires  Buenos Aires (BA, CF)
  • The first field is the ISO 3166 2-character country code. AR in the example stands for Argentina.
  • The second field is the latitude and longitude of the zone's principal location in ISO 6709 sign-degrees-minutes-seconds format, either +-DDMM+-DDDMM or +-DDMMSS+-DDDMMSS, first latitude (+ is north), then longitude (+ is east). In the example -3436-05287 stands for 34°36'S 52°87'W.
  • The third field is the most important. This is the zone name, and the one you have to remember. America/Argentina/Buenos_Aires in the example.
  • There may be a fourth field with comments that may help you identify other localities covered in the zone.

Now, having identified your timezone's name, add this line to the .htacccess file:

SetEnv TZ America/Argentina/Buenos_Aires

That's it! From now on, the scripts in your web server will run in your local time.

From Within Scripts

Depending on your server's configuration, PHP (CGI) or other script settings in your .htaccess files will be ignored by the server, and will require you to explicitly define your settings from within your scripts. Here are options to use within scripts to set the default timezone...

PHP

Use one of the following options in your PHP code before running any other date functions:

<?php putenv("TZ=your preferred timezone"); ?>

or...

<?php ini_set("date.timezone", "your preferred timezone"); ?>

or...

<?php date_default_timezone_set("your preferred timezone"); ?>


Remember to find out what your timezone is on the PHP Site.

MYSQL

For MYSQL in any case, set your preferred UTC/GMT timzone by running the following query before any other. Note that this query must be run at the start of each script file execution as the change lasts for only as long as your script runs or you're connected to the mysql server (example using the PHP mysql_query function, where -X- is your UTC/GMT hour offset)...

mysql_query("SET time_zone = '+X:00';");

See: MYSQL Timezone Help

See: Google GMT Clock


From Outside of Scripts

With PHPrc file

Advanced users only. This affects all PHP scripts that will run under your account and will use the PHP version phprc file you have modified.

IMPORTANT: See PHP.ini page for instructions on how to properly setup phprc before proceeding.

If you have sites that will require different timezone this may not be the optimal method unless you override the settings on a per domain basis.

To create phprc

  • SSH into your account
  • In the root user folder i.e. /home/username create a new directory named .php mkdir .php
  • Change to that directory cd .php
  • create a new directory 5.3 mkdir 5.3
  • change to the 5.3 directory cd 5.3
  • create the phprc file touch phprc
  • open the file for editing pico phprc (or your preferred editor)
  • add the line date.timezone=(your/timezone) http://us.php.net/manual/en/timezones.php
  • save the file
  • reapply (if needed) chmod 700 to phprc file

That's all there is to it. If you already have a phprc file you likely already know enough to just use the 2nd last line.