Multi Timezones App

Hello yii people out there,

i’m developing on a project wich involves multiple timezones. In fact it has a

auction system in which auctions are stored in a server with a timezone set to UTC,

while users are asked to specify their own timezone at registration time.

So we have to make timezone conversions each time a user gets or sets data involving a datetime (like auction expiration for example).

I used this approach and i would like to have your opinion on it, if it’s good, it’s bad, or if it can be improved.

I made a component autoloaded at startup:

class TimeZoneKeeper extends CComponent{

	public $userTimezone;

	public $serverTimeZone;

	public function init(){

		//settiamo il server di default a UTC per rendere l'implementazione  indipendente

		//dal settaggio del server.


		$this->serverTimeZone = new DateTimeZone(Yii::app()->getTimeZone());


	public function serverToUser($timestamp){

		$this->userTimezone = new DateTimeZone(Yii::app()->user->tz);

		$serverDateTime = new DateTime("@".$timestamp);

		$offset = $this->userTimezone->getOffset($serverDateTime);

		return ($serverDateTime->format('U') + $offset);


	public function userToServer($timestamp){

		$this->userTimezone = new DateTimeZone(Yii::app()->user->tz);

		$userDateTime = new DateTime("@".$timestamp);

		$offset = $this->userTimezone->getOffset($userDateTime);

		return ($userDateTime->format('U') - $offset);



it sets the application timezone to UTC regardless of the one set by the server.

it gets the timezone stored in the UserIdentity whatever it is.

then it provides two functions to convert datetime representations from the server timezone to the user’s and vice versa.

Any comment and ideas about it will be greatly appreciated!!

Best regards.

This is pretty much what I do. So far it seems to be perfect

well good to hear that… ;)

Sorry to dredge up an old post, but I’m running into a similar situation. From the look of the component, it would seem you would need to call it in the afterFind and beforeSave calls, perhaps automatically in a behavior. Am I understanding correctly or is there some cool Yii way of doing this that I can’t see?

Add something like this to the config

return array(


  'preload'=>array('timezonekeeper', ..., ...),


  // application components