How to change the date.timezone in Linux/PHP?

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.

  1. Login to the server as a "root" user, and check your server timezone by running Linux 'date' command.
  2. # date
    Thu Apr  7 11:35:34 CDT 2011
    

  3. Linux timezone is stored in the /etc/localtime. You'll need to swap this file with the correct timezone file of your choice.
  4. Change directory to the /usr/share/zoneinfo. You'll find a list of available timezones. Choose the one that closely match your timezone.
  5. 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.
  6. # ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
    

  7. Use 'rdate' or 'ntpdate' to synchronize your server time with reference time.
  8. # ntpdate clock.redhat.com
    

  9. Edit the ZONE entry in the /etc/sysconfig/clock file.
  10. Set the hardware clock by running the command below:
  11. # /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
Tags: 

Comments

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.