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.
Yii::app()->setTimeZone("UTC");
$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.