setState for timezone problem

hi, I don’t understand how to get the state valorized after an ajax call.

this is my index:

public function actionIndex() {

    	    	// Get timezone client

Yii::app()->clientScript->registerScriptFile("//", CClientScript::POS_END );

    	$urlTimezone = Yii::app()->createAbsoluteUrl('brief/timezone');

        Yii::app()->clientScript->registerScript('get_data_timezone', "

	mytimezone = function(){

              var tz = jstz.determine();

				var timezone =;


			  url: '".$urlTimezone."',

			  type: 'POST',

			  data: {tz: timezone}






", CClientScript::POS_END);

        // richiamo i contest con fase diversa da END

        $dataProvider = new CActiveDataProvider('VwContest', array(

            'criteria' => array(

                'condition' => 'end= 0 AND id_winner is null',



        $this->render('index', array(

            'dataProvider' => $dataProvider,



action ajax url:

public function actionTimezone() {

		$timezone = $_POST['tz'];

		// inserire valore del timezone in sessione

		Yii::app()->user->setState('timezone', $timezone);



my view:

$clientTimezone = Yii::app()->user->getState('timezone') ? Yii::app()->user->getState('timezone') : 'Europe/Berlin';


		$datetime = new DateTime($data->data_fine_fase );	

		$la_time = new DateTimeZone($clientTimezone);


I get timezone from the client but I need to refresh page twice because first it changes the session value, second it renders a view.

How can I get the value just when the client load the page first time?

// sorry for my english hope u understand well to help

help? yii experts?


Personally I have implemented this using cookies.

When it is the first visit, the page is refreshed if the cookie was not present.

As I am using cookies, I am not using state - the state is limited to the session.

Here is a partial implementation:


     * Instruments the browser with code to determine the browser's timezone.


     * Stores the value in a browser cookie (so that we get it on each request).

     * Can be configured to get the timezone through a post request.


     * @param string $route  The route to post the timezone to (if not null)

     * @param string $key  The key for the timezone value of the route.

     * @param string $cookiekey  the key to store the time zone in.


    public static function determineBrowserTimezone($forceCheck=false,$route=null,$key='tz') {

        // If cookie already set, do nothing

        if(!$forceCheck&&(self::$_hasTzScript||isset($_COOKIE[self::$timezoneCookieKey]))) return;

        self::$_hasTzScript=true;// Avoid adding script twice for speed.




        // Set cookie (if tz changed) and reload page

        $jsPost="jQuery.cookie('{$cookieKey}',timezone,{path:'/'/*,expires:new Date(((new Date()).getTime()+30000000000))*/});window.location.reload();";

        if($route!==null) {





            var tz = jstz.determine();

            var timezone =;

            var ck=jQuery.cookie('{$cookieKey}');









This could be optimized by :

  1. Avoid page rendering if the cookie is missing and just send a simple, essentially empty page to set the cookie;

  2. Reload the page only if the timezone information was needed on the current page (by checking the use of the local time conversion functions).

In practice the above is fine for me.