Date-related PHP can be one of the more… frustrating aspects of scripting. Whereas match calculations are the same no matter when or where you perform them, date calculations might appear to work properly in development and then fail later when you aren’t looking.

Solid Statement: PHP timezone settings are found in php.ini under date.timezone or inside your scripts with date_default_timezone_get() or date_default_timezone_set("America/New_York").

In this article, I wanted to provide some tips to help you safeguard against timezone-related errors, get or set your default timezone in PHP, and manage the setting if your web host doesn’t give you access to your PHP installation. I will also touch on DST. Daylight Savings Time for 2012 begins a week from today. DST tends to get developers to second-guess themselves.

What do I need to know?

If you are using any of PHP’s date functions, you need to be aware of the timezone of your PHP installation, the operating system’s time settings, and any differences in those values between your development, qa, and production servers.

PHP’s Default Timezone

The default timezone is UTC and it is set in the php.ini file. UTC is “Coordinated Universal Time,” formerly referred to as “Greenwich Mean Time” or GMT. This is the timezone for England. In the United States, the timezone is 5-8 hours different and should be set appropriately.

Below is the date section of the php.ini file. You can modify the date.timezone value with a valid entry from PHP’s list of valid timezones. This can be a code like EST or word pair like America/New_York.

; Module Settings ;
; Defines the default timezone used by the date functions
date.timezone = UTC
;date.default_latitude = 31.7667
;date.default_longitude = 35.2333
;date.sunrise_zenith = 90.583333
;date.sunset_zenith = 90.583333

Edit the php.ini File

The other entries in the php.ini date module are for location coordinates and sunrise times. They are not used in this discussion.

Edit your timezone on the line shown above, save the file, and restart the server for the changes to take effect.

Sometimes, however, you don’t have access to the php.ini file or a server restart. This is especially true when dealing commercial web-hosting companies.

In that case, PHP has functions to check and set the timezone in your script.

Get or Set PHP Timezone in a Script

// Get timezone from script
// Get timezone from PHP installation
// Set the timezone in the current script

When using the get(), PHP will first look for timezones set using set() function. If that is not set, it will return the value from php.ini.

Obviously, the ini_get() will pull the value directly from the ini file and will skip date_default_timezone_get(). This function is useful for seeing if you want to set the timezone in the first place.

Daylight Savings Time

Since the default PHP timezone is UTC, it knows nothing about DST (Daylight Savings Time). Therefore, you want to set your timezone using the correct string from the list of supported timezones.

If I set the date.timezone value to “America/Indianapolis” in my php.ini, then the system will automatically advance the time by 1 hour a week from today (at 2 AM on March 11, 2012).

Solid Tip: Remember, first and foremost PHP gets it time from the operating system. You must correctly set the time, timezone, and DST settings for your operating system or PHP will not display the correct time, no matter what calculations you are doing in your script.

Some Pointers to Remember

  • Always set your OS date and time settings correctly
  • Set your local timezone in php.ini (restart required)
  • Store time in UNIX timestamps
  • Only convert to local date/time at the last possible moment
  • Timezones and DST boundaries can and do change
  • Verify PHP shows correct dates after DST changes
  • DST changes occur in March and November