Karakas Online

27.13.2. How to adjust server time

Figure 27-13. Your Info profile: Timezone and Date settings.

Your Info profile: Timezone and Date settings.

Your Info profile: Timezone and Date settings.

In some cases, perhaps due a misconfiguration of your server, the time shown up in the News stories is wrong. Normally, it should be possible to control it by changing the time settings in either the Preferences of the administration panel, or the user preferences (Figure 27-13). However, there will be situations where a quick fix is desirable.

You can offset the time by tweaking the following line in the mainfile.php in the function formatTimeStamp (see How to adjust server time in PHP-Nuke):

$datetime = strftime(""._DATESTRING."", mktime($datetime[4],$datetime[5],$datetime[6],

For example to add one hour (3600 seconds), you would simply add 3600:

$datetime = strftime(""._DATESTRING."", mktime($datetime[4],$datetime[5],$datetime[6],
$datetime[2],$datetime[3],$datetime[1]) + 3600);

or, to subtract two hours:

$datetime = strftime(""._DATESTRING."", mktime($datetime[4],$datetime[5],$datetime[6],
$datetime[2],$datetime[3],$datetime[1]) - 7200);

A situation that calls for server time modification arises when your server is located in a different timezone than your geographic location - say you are located in Japan, but your hosting company is in Canada (see How to adjust server time in PHP-Nuke). The article on phpbuilder.com On User-Defined Timezones in PHP explains the problem in detail. Quoting:

PHP's 'mktime' and 'date' functions work well as a pair without the help of any other timestamp manipulation routines, but only if the application in which they are used is concerned solely with display and entry of time in the servers timezone. If an application needs to handle entry from a timezone other than that in which the server is located something more than 'mktime' and 'date' is required.

Two things are required to accomplish this: a location independent format for storing time in the database, and methods to translate to and from that format into the user's local time.

Read the original article for a solution. To apply it to PHP-Nuke, you would need to replace the time functions with the new ones, read the time offset from the user's profile and display the result.

To reflect the new time zone that may be implied by the hardcoded time offset, you would also want to change the _DATESTRING definition in your language file, e.g. language/lang-english.php:

define("_DATESTRING","%A, %B %d @ %T %Z");

You can change the "%A, %B %d @ %T %Z" string to whatever you deem appropriate. PHP gives to some placeholders a special meaning:

You can combine them with punctuation or extra words to construct the datetime string of your liking. You are not limited to the above interpretation. For example, you can just delete the %Z, and replace it with EST or whatever you time zone is (if you cannot set it from the preferences, that is). There are some points to bear in mind while experimenting:

See the PHP manual page for strftime for more information and examples about date string formatting.

Last updated Sun Aug 7 21:14:12 CEST 2005 Permalink: http://www.karakas-online.de/EN-Book/adjust-server-time.html All contents © 2004 Chris Karakas, Claudio Erba