Issue

PHP scripts return the wrong time of day, unless the time zone is set correctly.

This article explains how to set the correct time zone for IBM i and for PHP.

Environment

Zend Server for IBM i version 6 or higher.  IBM i, any currently supported version.  PHP, any currently supported version.

Resolution

There are several settings that affect the time zone, including the setting for your IBM i, the setting for FastCGI, and the setting for PHP.  This article tells how to make each of these settings, and also how to set the time zone in your script to override the default.  There is also a sample script that shows a formatted date and time.

Set the time zone for IBM i

To set the time zone on your IBM i, please set the system value QTIMZON. Some available values for the U.S. are:

Time zone descriptionNew 2007 rules QTIMZON value
Atlantic TimeQN0400AST2
Eastern TimeQN0500EST3
Central TimeQN0600CST2
Mountain TimeQN0700MST3
Pacific TimeQN0800PST2
Alaska TimeQN0900AST2
Hawaii-Aleutian Time

QN1000HAS2

For example, if you are in the Pacific time zone, you would use this command:

CHGSYSVAL SYSVAL(QTIMZON) VALUE(QN0800PST2)

If you change this value, you should also reset system value QTTIME to the correct time, using a six digit 24 hour time. For example, if the time is 4:35 in the afternoon:

CHGSYSVAL SYSVAL(QTIME) VALUE('163500')

You can find a complete list of supported time zones at IBM i info center - Timezone

In the US, select a time zone that shows a DST start of "Second Sunday in March at 02:00 a.m." to make sure you are using the current DST convention.

Set the default time zone TZ variable in the FastCGI configuration



Starting with Zend Server 2020, we no longer set the TZ setting in fastcgi.conf.


If the value for TZ does not match your other settings, you will be able to notice this discrepancy in the time displayed on the Zend Server User Interface in your browser.  After making the correction in fastcgi.conf, restart Apache and log out and back in to the User Interface to see the changed time.

Usually the installer takes care of this setting, but you can check to verify it, and adjust if needed. Please find this file and open for browsing/editing:

For Zend Server 9 and 2019:

/www/zendphp7/conf/fastcgi.conf

For Zend Server versions 6 through 8.5:

/www/zendsvr6/conf/fastcgi.conf

This is a configuration file, so if you do make any changes, please BACK IT UP first.  That way, if any change makes it not work, you can replace the changed file with the back up and get running again quickly.  If you make any changes to this file, you must restart Apache for the change to take effect.


The fastcgi.conf has a very long first (non-comment) line.  Everything on this line has to be on the first line, so it has to be this long.  Please be careful not to inadvertently insert any line breaks into this line.
That way, if any change makes it not work, you can replace the changed file with the back up and get running again quickly.  If you make any changes to this file, you mustrestart Apachefor the change to take effect.

Out at the very end of this line, you can usually find the TZ environment setting.  If it is not at the end, check the whole line to see if it is anywhere else.  If there is no TZ setting, you should add one.

The clause that sets the TZ environment variable will look similar to this one:

SetEnv="TZ=<PST>8<PDT>,M3.2.0,M11.1.0"

This example shows that the standard time zone is Pacific Standard Time (PST), the offset from UTC (or CUT) is 8 hours, and the daylight time zone is Pacific Daylight Time (PDT).  The next two items define the start and end date for daylight savings time in a month, week, day of the week format.  So, daylight savings starts in the third month (March), the first week of March, and on Sunday, the zero day of the week. 

You can learn more about setting the TZ variable and see charts of the available time zones here:

IBM Knowledge Center - environment File

You don't need to set up an environment file, so ignore all that.  This is just the page that seems to have the best documentation of the TZ variable, so please look at that information.

One important difference in the way the documentation presents the time zones is that they do not use the less than and greater than signs (<>) on the time zones.  We use them in the FastCGI configuration because it somehow improves the ability of PASE to parse them correctly.  So, for example, in the documentation you would see this for Central Europe:

CET-1CEST

But in the fastcgi.conf file, you would add the punctuation, like this:

<CET>-1<CEST>

Here are some examples for the continental U.S.:

TimezoneValue to use
ETSetEnv="TZ=<EST>5<EDT>,M3.2.0,M11.1.0"
CTSetEnv="TZ=<CST>6<CDT>,M3.2.0,M11.1.0"
MTSetEnv="TZ=<MST>7<MDT>,M3.2.0,M11.1.0"
PTSetEnv="TZ=<PST>8<PDT>,M3.2.0,M11.1.0"


If the value for TZ cannot be parsed correctly, then TZ will default to GMT/UTC.  You will be able to notice this discrepancy in the time displayed on the Zend Server User Interface in your browser, and in the PASE time from the example code provided below.

Set the default time zone for PHP

To set the time zone for PHP, For Zend Server, please go into the Zend Server User Interface in your browser, navigate to Extensions -> PHP, find 'date' on the list, and single-click it to expand the directives.

Set the time zone (date.timezone) using the country/city code appropriate for your area.  Here are some examples for the continental U.S.:

TimezoneValue to use
ETAmerica/New_York
CTAmerica/Chicago
MTAmerica/Denver
PTAmerica/Los_Angeles

For example, if you are in the Pacific time zone, use the value America/Los_Angeles. Do not enclose the value in quotes. You must restart Apache for this change to take affect.

You can find a complete list of supported time zones in the PHP manual at List of Supported Time Zones .

Log out of the Zend Server User Interface and log back in

After any changes to the time settings are made, you may need to log out of the Zend Server User Interface and log back in to see the correct time displayed there.

Setting the time zone in your script

You can override the default PHP time zone by using the date_default_timezone_set() function in your script. Details can be found in the PHP Manual at  PHP Manual date_default_timezone_set function .

Simple script to demonstrate the time zone setting

This simple script will present the date and time nicely formatted.  It can be useful to demonstrate that the time zone settings are working correctly.

<?php
// Example to show date and time in a nice format
echo "The current date and time is ";
echo date("F j, Y, g:i a");
echo ", in the timezone ";
echo date("e");
// Show date and time from PASE
echo '<br>PASE time: ';
echo `/QOpenSys/usr/bin/date`;
?>

An example of the output:

The current date and time is June 29, 2016, 9:01 am, in the timezone America/Los_Angeles
Wed Jun 29 09:01:59 PDT 2016