3

Running web scripts in your timezone

 3 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/217185277-Running-web-scripts-in-your-timezone
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Running web scripts in your timezone

Overview

Many web programs log or show the time. If these programs use the local time, it shows the US West Coast Timezone (PDT/PST) that DreamHost uses for its servers.

However, you can change that for your scripts. View the following link to view a world timezone map:

Finding your timezone

You can find a list of known timezones on your server. You must first create a shell user, then login via SSH. You can then run the following command to view all timezones configured.

[server]$ cat /usr/share/zoneinfo/zone.tab

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 appears in this example.
  • There may be a fourth field with comments that may help you identify other localities covered in the zone.

You can also check this list from Wikipedia for further information:

Changing the timezone in a PHP script

Use one of the following options in your PHP code before running any other date functions. The following examples both use 'America/Argentina/Buenos_Aires' as the timezone.

ini_set("date.timezone", "America/Argentina/Buenos_Aires");
-or-
date_default_timezone_set("America/Argentina/Buenos_Aires");

You can find your timezone by visiting the following page:

Changing the timezone in a php.ini file

Additionally, if you want all of your sites under a user to run in a specific timezone, you can create a phprc file. In that file, add the following line:

date.timezone=(America/Argentina/Buenos_Aires)

Kill off all running PHP processes as stated in the article. When you finish, all PHP scripts use the timezone specified in this file.

MySQL

For MySQL, set your preferred UTC/GMT timezone by running the following query before any other. This example uses the PHP mysql_query function, where -X- is your UTC/GMT hour offset). For example, you would use -5 for New York city since it's 5 hrs behind GMT.

mysql_query("SET time_zone = '-5:00';");

This query must run at the start of each script execution as the change lasts for only as long as your script runs, or you're connected to the MySQL server.

For more information, please visit:

Viewing the time PHP uses

Create a new file named something like date.php with the following code:

<?php
echo date('l jS \of F Y h:i:s A');
?>

Visit this page in a browser to view the time your PHP code is using.

See also

Did this article answer your questions?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK