2013-02-02 40 views
1

我無法理解如何將日期時間保存到數據庫。我有串Symfony2,學說,如何將日期時間設置爲實體

(string) $oXml->currentTime 

實際上它不是一個字符串,但我們把它轉換,所以,我怎麼可以將其添加到實體沒有錯誤

Fatal error: Call to a member function format() on a non-object in... 

當前代碼

$currentTime = \DateTime::createFromFormat('Y-m-d H:m:s', (string) $oXml->currentTime); 
    $cachedUntil = \DateTime::createFromFormat('Y-m-d H:m:s', (string) $oXml->cachedUntil); 

    $oApiKeyInfo 
      ->setCurrentTime($currentTime) 
      ->setCachedUntil($cachedUntil) 

不工作:(

回答

1

你需要傳遞一個DateTime對象,用一個新的語句創建它,你可以指定使用的時間第一個構造函數參數。

$currentTime = new \DateTime((string) $oXml->currentTime); 
$cachedUntil = new \DateTime((string) $oXml->cachedUntil); 

$oApiKeyInfo->setCurrentTime($currentTime) 
    ->setCachedUntil($cachedUntil); 

如果你需要指定你可以使用DateTimeZone類,並把它作爲第二個參數DateTime構造一個時區。

+1

thnx解決方案處於「設置時區」 – user1954544

相關問題