2011-04-08 19 views
1

在我的應用程序中有一個默認時區。每個用戶都有自己的時區。我想設置存儲在數據庫中的用戶時區。在Bootstrap文件中設置時區

如何根據Bootstrap文件中的用戶設置時區。

在此先感謝...

class Crm_Bootstrap extends Zend_Application_Module_Bootstrap { 

    protected function _initTimezone() 
    { 

     $userObj = new Application_Model_DbTable_Users(); 
     require_once('Zend/Session.php'); 
     $session = new Zend_Session_Namespace('logincred'); 
     $userdetails = $userObj->userdetails($session->user); 
     date_default_timezone_set($userdetails[0][id]); 
    } 
} 

回答

0

創建initTimezone()方法的引導,從數據庫中檢索用戶/會話的時區,並設置時區,如果用戶沒有登錄,設置默認時區。

+0

請參閱我的問題我已經添加了我的代碼。如果這是錯誤的,請爲我提供正確的代碼。感謝您的幫助... – 2011-04-08 11:57:33

+0

它工作嗎?這個代碼存在一些問題/問題,許多事情都以不理想的方式解決。首先:你爲什麼不使用自動裝載機? – markus 2011-04-08 12:10:39

+0

也嘗試使用自動加載器。當試圖使用'$ userObj = new Application_Model_DbTable_Users(); require_once('Zend/Session.php'); require_once('Zend/Session.php'); $ session = new Zend_Session_Namespace('logincred'); '我只得到空白的白色屏幕。 – 2011-04-09 09:57:55

0

您還需要在用戶的登錄方法以及引導程序中執行此操作,否則成功登錄後的初始瀏覽量會有不正確的時區。

public function login($username, $password) { 
    // .. login code .. 
    // .. login successful .. 
    date_default_timezone_set($user->timezone); 
} 
+0

請參閱我的問題,我添加了我的代碼。如果這是錯誤的,請爲我提供正確的代碼。感謝您的幫助... – 2011-04-08 11:53:38

+0

我會假設'$ userdetails [0] [id]'是不正確的,那肯定會讓你的用戶ID?您需要獲取時區列,並將列名稱用引號括起來,而不是使用常量。例如:'$ userdetails [0] ['timezone']' – 2011-04-08 11:58:46

+0

好吧,這是一個錯字錯誤。但我試着用$ userdetails [0] ['timezone']。 – 2011-04-08 12:02:22

0

蔭使用doctrine2 + Symbony 檢查用戶名行中數據的基礎上。

$userRepo = $em->getRepository('modules\user\models\Users')->find($userId); 

取時區在你的數據庫,並放置值函數

$timezone = $userRepo->timezone; 
date_default_timezone_set($timezone); 

代碼

class Crm_Bootstrap extends Zend_Application_Module_Bootstrap { 

    public function init() 
    { 

     $em = $this->getEntityManager('user'); 
    @$userId = Zend_Auth::getInstance()->getIdentity()->id; 
    $userManager = new UsersManager(); 
    if ($userId) { 

     $userRepo = $em->getRepository('modules\user\models\Users')->find($userId); 
     date_default_timezone_set($userRepo->timezone); 

    } 
    } 
} 

你在數據庫表中創建時區場