Manual talk:Timezone

Background
This article was inspired by a recent install of MediaWiki and an unsuccessful attempt to set the global Timezone to something other than UTC. After finding several broken, incomplete and incorrect pages it became apparent that a Help:Timezone page was badly needed.

The primary working method was taken from a previous article and from this wikitech email authored by Brion Vibber.

It was tested on a system with the following Versions: * MediaWiki: 1.5.6 * PHP: 4.3.11 (apache) * MySQL: 4.1.13-standard

The unconfirmed methods were tested unsuccessfully on the same system but included for further testing. Locker 09:55, 2 March 2006 (UTC)

I tried these on MediaWiki: 1.6.7, PHP: 4.3.5 (cgi), MySQL: 4.1.11-Debian_4sarge2-log, CEST time zone. By default, both logs and signatures show UTC. --Tgr 07:21, 20 July 2006 (UTC)
 * Using the primary method (with "Europe/Budapest") does absolutely nothing (except for changing the "(UTC)" text in the signature).
 * Adding Date("I") as suggested does nothing either.
 * Using only the $wgLocalTZoffset = date("Z") / 3600; line and nothing else leaves server time unchanged, and sets local time to server time +2. This gives correct CEST time in recentchanges and logs (including the deletion log; I dind't test the upload log), but still UTC in signatures.

My host probably didn't support the tz database. Primary method works fine, but I had to support time zone and DST manually. Rules for that can be found here. --Tgr 08:54, 20 July 2006 (UTC)

Just two lines are required in LocalSettings.php
Using these two lines only (for e.g. New Zealand Date Time) works a treat $wgLocaltimezone = "Pacific/Auckland"; $wgLocalTZoffset = date("Z") / 60; It fixes the incorrect displaying of Extension:RSS, along with all history pages (e.g. upload, deletion, recent pages logs).

--131.203.252.214 00:30, 6 July 2010 (UTC)

This worked for me. -- Gishu Pillai (Apache/2.2.17 (Win32) PHP/5.3.6 DAV/2 MySql 5.5.11)

Interestingly enough the above didn't work for me, also in New Zealand. What did work for me is: $wgLocaltimezone = "Asia/Jakarta"; date_default_timezone_set( $wgLocaltimezone ); Using bitnami-mediawiki-1.21.1-1-windows-installer.exe which includes (Apache/2.4.6 (Win32) PHP/4.0.5 MySql 5.5.32)
 * 1) Set Default Timezone

Use localtime
I'm using MediaWiki 1.6.6. I have tried the method mentioned in Help:Timezone and the time displayed successfully in history and recentchanges. The signature is not affected, but it always shows the correct time, regardless I change the $wgLocaltimezone or not. How is the mechanism in MediaWiki so that there are two different time functions? How to fix this? It is very critical because whenever I use the magic word, it points to the wrong time. TIA --Semut 13:18, 1 June 2006 (UTC)
 * always shows UTC time. Use instead. See: w:en:Help:Variable -- Mårten 193.200.150.167 18:28, 26 October 2008 (UTC)

Server May Crash..!! 
It works, but the server may crash.

My environment:


 * Windows XP sp2
 * AppServ 2.5.6
 * Apache: 2.2
 * PHP: 5.1.4 (apache2handler)
 * MySQL: 5.0.22-community-nt-log)
 * MediaWiki: 1.8.0

--Chlon 03:55, 16 October 2006 (UTC)

Really!
I confirm the existence of that problem. When I tried that primary method I got error: "An unhandled win32 exception occured in Apache.exe [2320]." My environment:


 * Windows XP sp1
 * Apache: 2.0.59 (Win32)
 * PHP: 5.2.0
 * MySQL: 5.0.27-community-nt
 * MediaWiki: 1.8.2

Experimentally I found out, that probably the line  is causing problem. CodeMonk 00:03, 28 November 2006 (UTC)

Confirmed
I can also confirm site failure (PHP has encountered an Access Violation at 7C81BD02) when this directive is set.


 * OS: Windows Server 2003 SP2
 * Web: Internet Information Services 6
 * PHP: 5.2.1
 * MySQL: 5.0.37-community-nt
 * MediaWiki: 1.10.1

IIS and MySQL must be restarted to clear this error.

Does not work on my MediaWiki 1.10.0 upgrade!
Used the code to no avail. It does not work since I upgraded from 1.6.10. $wgLocaltimezone = "Asia/Jerusalem"; $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone"); $wgLocalTZoffset = date("Z") / 3600; putenv("TZ=$oldtz");
 * 1) Versions before 1.7.0 used $wgLocalTZoffset as hours.
 * 2) After 1.7.0 offset as minutes

date_default_timezone_set("Asia/Jerusalem"); I'm lost now. Nate, 89.1.57.35 23:51, 6 June 2007 (UTC)


 * Signatures on talk pages will show correct time, Special:Recentchanges shows the same edit using wrong time! Nate, 89.1.57.35 00:08, 7 June 2007 (UTC)


 * I don't know what happened, but as I was playing with editing it, putting stuff on an off again, suddenly this agreed to work.

$wgLocaltimezone = "Asia/Jerusalem"; $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone"); $wgLocalTZoffset = date("Z") / 60; putenv("TZ=$oldtz");
 * 1) Versions before 1.7.0 used $wgLocalTZoffset as hours.
 * 2) After 1.7.0 offset as minutes
 * Date("Z") returns seconds, previous division by 3600 was giving hours, but as the specification says we are now to use minutes - division by 60. Funny thing is I already tried it before. Don't know why this time it caught on. Very weird. Nate, 89.1.57.35 02:06, 7 June 2007 (UTC)

Seriously?
Is there really a 5 line php-patch necessary to display the time normally? Isn't there a Special:Timezone page yet, where i can tell the wiki to use the servertime?

Server time != mediaWiki-time
Hi there, my server seems to be in the correct time, correct time zone and tht php.ini is adjusted as well to my timezone. The mediaWiki on the server however displays in the preferences, that the server is minus two hours of what it shows. How can this be? I am in Europe/Berlin timezone. --Thekryz 16:50, 24 May 2008 (UTC)
 * It seems that the server time that the MediaWiki displays, always is the UTC time. -- Mårten 193.200.150.167 18:53, 26 October 2008 (UTC)
 * Same for me. Here are some strings from my LocalSettings.php, try to guess why I commented all of them out:

//$wgLocaltimeZone="Europe/Moscow"; /*oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone"); $wgLocalTZoffset = date("Z") / 60; putenv("TZ=$oldtz");*/ //$wgDefaultUserOptions['timecorrection'] = '05:00'; //$wgLocaltimezone = "UTC+3,M3.4.0/2,M10.4.0/3"; //last sunday march 2:00+1, last sunday oct 3:00-1 //$wgLocalTZoffset = date("Z") / 60; //$wgLocaltimezone="UTC+3"; //$wgLocaltimeZone=":Europe/Moscow";
 * Neither works, MW is still in UTC. What else can I try? Help me please. MW 1.13.2, PHP 5.2.9, Windows 2003 Server.

I confirm this issue. Running MW v1.15.1 on Apache2 with PHP v5.3.2. I've set: $wgLocaltimezone="Europe/Stockholm"; $wgLocalTZoffset = date("Z") / 60; Also, I've added: date.timezone = "Europe/Stockholm" to /etc/php5/apache2/php.ini, so that shouldn't be the issue. Also, PHP date function returns correct CEST time. Also, system itself (Ubuntu 10.04) returns correct CEST time. Weird. I see now that my MW is quite old; i.e. 1.16.x is out. Any chance this is all fixed? Cox 18:46, 11 April 2011 (UTC)

The term "Server time" is quite misleading, it should probably read "Server time in UTC". If php date shows the correct time and zone, everything should be fine. --84.175.68.222 19:51, 25 October 2013 (UTC)

Daylight saving time
Hi there, I tried every method described in this manual to make my MediaWiki 1.8.2 on Windows XP SP2 take daylight saving time into account, but didn't succeed. The problem was not in wrong server timezone, PHP date function returned correct time. The fact is that MediaWiki uses UTC time internally, and every user has his own time shift. So if user's local time is re-adjusted because of daylight saving time, every user must manually re-adjust that shift in individual settings. That's a correct way, but it can't be done automatically. The wrong way will be to adjust user's time shift during daylight saving time automatically, though daylight saving is not used in many countries. In that case adjusting $wgLocalTZoffset variable won't help (and there is also a bug in userAdjust function in Language.php in my 1.8.2), so this must be done in Language.php file:

} else { $hrDiff = intval( $tz ); } $hrDiff += date("I"); if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
 * 1) No difference ? Return time unchanged

Just add the bold line in that code fragment. After that, user's time shift will be silently re-adjusted during daylight saving time, as it is set on the server. I guess, this will work not only in 1.8.2. Best regards, CodeMonk 02:30, 18 June 2008 (UTC)

Thanks. This works with 1.20.0:

$minDiff += 60; if ( 0 == $minDiff ) { return $ts; }
 * 1) No difference ? Return time unchanged

maybe it's better to expand the function with:

function userAdjust( $ts, $tz = false ) { global $wgUser, $wgLocalTZoffset, $wgdaylightsavetimecorrection; ...

and add to LocalSettings.php:

$wgdaylightsavetimecorrection = 60;

and

$minDiff += $wgdaylightsavetimecorrection;

as above...

DST on server in your own timezone
If you have your MediaWiki on a server in your own timezone, nothing mentioned in this article, but the following line in LocalSettings.php was needed for me to get the correct time, including correct behaviour during DST shifts (daylight saving time): $wgLocalTZoffset = date("Z") / 60; Then you don't have to use $wgLocaltimezone to get correct times on signatures, since MediaWiki uses the server's time by default if $wgLocaltimezone is not defined, and the server normally knows when to go on/off DST. However $wgLocalTZoffset has to be used as above to get correct times on edits, since the default value on $wgLocalTZoffset is UTC time. Since date("Z") picks the time from the server, again DST is taken care of duly. -- Mårten 193.200.150.167 18:53, 26 October 2008 (UTC)

Symbolic time zone names
According to this (and my experience :-)), symbolic time zone names like "Europe/Berlin" have to be preceded by a ":" when used as a TZ setting, i. e. ":Europe/Berlin". Many libraries accept the variant without the colon as well, but I prefer playing it safe :-). --Tim Landscheidt 12:12, 6 April 2009 (UTC)


 * As I have problems with the timezone on Windows, I tried setting wgLocaltimezone to ":Europe/Berlin", but it definitly does not work on MediaWiki 1.23 on Windows. Tony Mach (talk) 12:57, 26 August 2014 (UTC)

Idea using DateTime and DateTimeZone
I think the following idea might work across different timezones and saving times:

$wgLocaltimezone = "America/Sao_Paulo"; $dtz = new DateTimeZone($wgLocaltimezone); $dt = new DateTime('now', $dtz); $wgLocalTZoffset = $dtz->getOffset($dt) / 60; unset($dtz); unset($dt);

Comments?

verified
[Mark.Yin] I used this solution on my wiki. It works well. My timezone is Asia/Beijing

[Anirudh] This works for me as well - my timezone is  Asia/Kolkata

[mykro] This works for v1.19.2 on Windows Server 2008 R2 in Australia/Adelaide timezone.

Given the multiple verifications I have elevated this to an alternative method in the article.


 * Does not work for me for MediaWiki 1.23 on Windows/XAMPP, and neither does a simple  work. Tony Mach (talk) 13:00, 26 August 2014 (UTC)

What works for me on Dreamhost
I faffed around with this for ages, before finally getting the following working on Dreamhost: MediaWiki 1.16.0 (r79310), PHP 	5.3.5 (cgi-fcgi), and MySQL 	5.1.39-log.

$wgLocaltimezone = "Australia/Perth"; $wgDefaultUserOptions['timecorrection'] = '08:00';
 * 1) For signatures (and no where else, it would seem):
 * 1) For log entries, recentchanges, page footers, and probably other places:

Changing  did nothing to anything, ever. :-(

Samwilson 04:11, 14 April 2011 (UTC)

No $_ENV, getenv, putenv on OS X.
$_ENV is blank on OS X. That means most of the code is irrelevant and useless. 202.89.188.28 06:35, 30 January 2012 (UTC)

Simply wont work
I have tried all possible combinations of the solutions presented above, using $wgLocaltimezone, $wgLocalTZoffset, $wgDefaultUserOptions['timecorrection'], etc...

Nothing seems to have absolutely no effect at all... My wiki is hosted at Dreamhost, but all users will access it from "America/Sao_Paulo". I have been trying to edit the LocalSettings.php and refresh my wiki page to see if the date of the "last changed" footer on the main page changes... but it doesn't. Don't know if it some kind of cache that is messing things up.

I am about to give up and simply ask all users to change their personal settings...

any ideas?

Rbirmann (talk) 22:35, 12 March 2013 (UTC)

Just an update. Maybe I was doing something wrong before, but I seem to have been able to work around it using the following line on my LocalSettings.php:

$wgDefaultUserOptions['timecorrection'] = '-03:00';

Hope it helps! Rbirmann (talk) 22:35, 12 March 2013 (UTC) Not working when upgrade mediawiki 1.13 into 1.19.--By Sephirothindra 03:53, 18 September 2013 (UTC)

Are we able to confirm?
The following suggestion from the "Unconfirmed Methods" section seems to be the only working option for me which also considers summer and winter time. It changes timestamps in signatures, recent changes and version history. No  and   voodoo required which was not working anyway.


 * MW 1.20.8, PHP 5.3.3 with the server on UTC ✅ --&#91;&#91;kgh&#93;&#93; (talk) 19:01, 28 November 2013 (UTC)

Cases where no special settings are required?
Should this manual page note that no special settings are required if the PHP function  already returns the desired value of   by default? If this is the case (as on the server I am working with) the following lines in Setup.php do the same thing as the Primary Method.

/** * Set up the timezone, suppressing the pseudo-security warning in PHP 5.1+ * that happens whenever you use a date function without the timezone being * explicitly set. Inspired by phpMyAdmin's treatment of the problem. */ if ( is_null( $wgLocaltimezone) ) { wfSuppressWarnings; $wgLocaltimezone = date_default_timezone_get; wfRestoreWarnings; }

date_default_timezone_set( $wgLocaltimezone ); if( is_null( $wgLocalTZoffset ) ) { $wgLocalTZoffset = date( 'Z' ) / 60; }

--Occidens (talk) 00:59, 23 April 2014 (UTC)

It really doesn't like GST
I wanted to set my wiki to Gulf Standard Time - GST. It really didn't like it.

Is there a configuration file somewhere, like TZ, where you can add timezones that it doesn't know about?

Just change a browser to try
I tried all the methods on the page and it just dosen`t work at all.

It always show the wrong time on my wiki page.

Then I change a PC to surf my wiki and found it show the right time on pages.

So I found that it cost me several hours for nothing.I should at first change a browser to try.

Timezone resets
If I change the system time on the server mediawiki will also change time zones, but once a edit is made it changes the system time back to what it was. Example mediawiki says it 12:00 I set it 11:00 and refresh. page says it's 11:00 I edit the page page says it's 12:00, solved by restarting server.

Uh
How do I change the timezone of my wiki without having to download the file..? 73.44.43.228 02:54, 11 August 2015 (UTC)


 * Just access the LocalSettings.php file from a command line editor like VIM, add the lines required, and save it in place. --Chazbot7 (talk) 16:31, 2 June 2016 (UTC)

Using en.wikipedia.org, it seems that  gives the same time as. How is it supposed to know the time zone? Gah4 (talk) 03:00, 9 November 2016 (UTC)