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) are located in a 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 time zones. Choose the one that closely matches 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 the 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 a 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 its own timezone and uses the default timezone when a 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 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 the 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

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment