If your date.timezone is not properly set, your web application will not display your local time correctly. The default date.timezone will be set to the server time zone. If you (or your company) is located in different time zone from the server, you may have to override the value in your PHP application.. The date.timezone can be set at the system level, PHP level or application level.
Linux Server Timezone
To set the date.timezone value globally within the server, you'll have to follow the steps outlined below.
- Login to the server as a "root" user, and check your server timezone by running Linux 'date' command.
- Linux timezone is stored in the /etc/localtime. You'll need to swap this file with the correct timezone file of your choice.
- Change directory to the /usr/share/zoneinfo. You'll find a list of available timezones. Choose the one that closely match your timezone.
- Remove /etc/localtime, and make a symbolic link to the timezone you wish to set. Since we are located in Chicago, we'll use link America/Chicago as shown below.
- Use 'rdate' or 'ntpdate' to synchronize your server time with reference time.
- Edit the ZONE entry in the /etc/sysconfig/clock file.
- Set the hardware clock by running the command below:
# date Thu Apr 7 11:35:34 CDT 2011
# ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
# ntpdate clock.redhat.com
# /sbin/hwclock --systohc
PHP default Timezone
PHP maintains it's own timezone, and uses the default timezone when date request is made within PHP. PHP timezone is stored in the php.ini file, and you may edit this file to change the default timezone.
[Date] ; Defines the default timezone used by the date functions date.timezone = "America/Chicago" date.timezone = "US/Central" // Preferred
To make the change effective, you'll have to restart the web server.
PHP application Timezone
Unless you're administering the server yourself, you have a limited control over the default date.timezone configured on the server. You may wish to change the default ("server") timezone within your PHP application to the local timezone where you reside. To retrieve or set default timezone within PHP, you'll need two function calls as noted below:
echo date_default_timezone_get(); date_default_timezone_set("America/Chicago"); date_default_timezone_set("US/Central"); // Preferred
Comments
Add new comment