Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Changing Time Zones

Go to End

2 Posts   2958 Views


Community Member, 70 Posts

23 April 2009 at 5:07pm

Edited: 23/04/2009 5:08pm

I have a site which makes heavy use of dates. The site has an NZ audience but is hosted with MediaTemple in the US. I needed to make it work so that dates were both stored in the database and displayed in the New Zealand time zone. All seems fine so far, so I am sharing how I went about this. (This is for SS 2.3.1 - I haven't tested on other versions).

As I cannot change the time on the server itself, I had to change the time zone in two places:

1) PHP
Luckily this web host allows you to add in PHP configuration directives into the .htaccess file, so I added this to the end of the site's .htaccess file:
SetEnv TZ Pacific/Auckland

A full list of php time zones can be found here:
I would also advise viewing the output of a phpinfo() call (and checking what is output under the Date header) to double check it has been set correctly for your site. You could also check the output of something like "echo date("l, F d, Y H:i" ,time());"

2) MySQL
This is slightly trickier if you don't have access to the MySQL server config (and I am assuming you don't). What you essentially need to do is set the timezone on a per connection basis before any other queries are made to the database. Unfortunately in this case, this means editing the following file: sapphire/core/model/MySQLDatabase.php

As this is a core file, be aware that changes incorrectly made could break your site, and if you upgrade silverstripe, it will be overwritten. I have submitted an bugfix with this change:

At line 41 underneath this line:

$this->database = $parameters['database'];
, I added the following code:
if($parameters['timezone']) {
			mysql_query("SET SESSION time_zone='" . $parameters['timezone'] . "'");

Then in my _config.php file, I added the following line to my $databaseConfig:

"timezone" => "+12:00",

so a sample $databaseConfig looked like this:

$databaseConfig = array(
	"type" => "MySQLDatabase",
	"server" => "localhost", 
	"username" => "test", 
	"password" => "", 
	"database" => "test",
	"timezone" => "+12:00",

Setting the time zone numerically can cause issues with daylight savings, but there is no viable alternative unless time zones have been loaded into the MySQL server, and you will not know this unless you have full config control over your database.

Applying the above changes should hopefully allow you to work with dates and times in a time zone of your choosing regardless of where your server is located.


Community Member, 904 Posts

23 April 2009 at 6:37pm

Hi rbquirke

Nice writeup, thanks for sharing this. The timezone parameter looks like a useful addition, however I'd slightly modify your code to:

// use the isset function instead of directly accessing the array index
if(isset($parameters['timezone'])) { 
	mysql_query("SET SESSION time_zone='" . $parameters['timezone'] . "'");

That will prevent php from displaying strict warnings (NOTICE) when the timezone parameter isn't set.
Strange thing that MediaTemple doesn't allow changing of timezone in their control panel? They probably have several customers from around the world...