2013-07-01 97 views
5

目前我已將用戶時區保存在他們的數據庫行中,並且每次打印日期時都會將其轉換爲用戶的時區。我怎樣才能以乾的方式做到這一點?在Laravel 4中更改用戶時區的最佳方法是什麼?

我應該重寫Eloquent返回Carbon DateTime對象的位置嗎?如果是這樣,我應該把它放在下面的特徵中,所以我只需要寫一次呢?

<?php 

use Carbon\Carbon; 
use Illuminate\Database\Eloquent; 

trait ConvertTimeZone { 
/** 
* Return a timestamp as DateTime object. 
* 
* @param mixed $value 
* @return DateTime 
*/ 
protected function asDateTime($value) 
{ 
    // If this value is an integer, we will assume it is a UNIX timestamp's value 
    // and format a Carbon object from this timestamp. This allows flexibility 
    // when defining your date fields as they might be UNIX timestamps here. 
    if (is_numeric($value)) 
    { 
     return Carbon::createFromTimestamp($value); 
    } 

    // If the value is in simply year, month, day format, we will instantiate the 
    // Carbon instances from that fomrat. Again, this provides for simple date 
    // fields on the database, while still supporting Carbonized conversion. 
    elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) 
    { 
     return Carbon::createFromFormat('Y-m-d', $value); 
    } 

    // Finally, we will just assume this date is in the format used by default on 
    // the database connection and use that format to create the Carbon object 
    // that is returned back out to the developers after we convert it here. 
    elseif (! $value instanceof DateTime) 
    { 
     $format = $this->getDateFormat(); 

     $timezone = \Auth::user()->timezone; 

     return Carbon::createFromFormat($format, $value, $timezone); 
    } 

    return Carbon::instance($value); 
} 

}

回答

6

我會創造一個BaseModel類,擴展Eloquent,從中我延長車型我需要這樣的功能。只需要記住檢查用戶是否已登錄,以便我們可以獲得其時區。例如:

型號/ BaseModel.php

class BaseModel extends Illuminate\Database\Eloquent\Model { 

    protected function asDateTime($value) { 

     // If Carbon receives null, it knows to use the default timezone 
     $tz = null; 

     // If the user is logged in, get it's timezone 
     if (Auth::check()) { 
      $tz = Auth::user()->timezone; 
     } 

     // If this value is an integer, we will assume it is a UNIX timestamp's value 
     // and format a Carbon object from this timestamp. This allows flexibility 
     // when defining your date fields as they might be UNIX timestamps here. 
     if (is_numeric($value)) { 
      return Carbon::createFromTimestamp($value, $tz); 
     } 

     // If the value is in simply year, month, day format, we will instantiate the 
     // Carbon instances from that fomrat. Again, this provides for simple date 
     // fields on the database, while still supporting Carbonized conversion. 
     elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) { 
      return Carbon::createFromFormat('Y-m-d', $value, $tz); 
     } 

     // Finally, we will just assume this date is in the format used by default on 
     // the database connection and use that format to create the Carbon object 
     // that is returned back out to the developers after we convert it here. 
     elseif (! $value instanceof DateTime) { 
      $format = $this->getDateFormat(); 

      return Carbon::createFromFormat($format, $value, $tz); 
     } 

     return Carbon::instance($value); 
    } 
} 

型號/ user.php的

class User extends BaseModel { 
    // ... 
} 
+0

請您爲什麼延伸的BaseModel比使用性狀最好告訴我?謝謝 –

+0

這不是更好,特質的東西其實是相當不錯的,而且工作原理也一樣。自從我開始與Laravel合作以來,這就是我一直在做的事情,我想我可以分享它。當然,這樣做的唯一好處是它不需要PHP 5.4.0,但這可能不是一個主要問題。 – rmobis

+0

只是使用增變器可能會更好。 –

相關問題