Laravel/Carbon 中的多个时区
Multiple Timezoning in Laravel/Carbon
我想知道这是否可行,假设我有这样一个模型:
MyModel
SomeDate - Carbon
现在,我也有一个当前用户的时区,如下所示:
User
MyTimezone
数据库中存储的时区始终以 UTC 格式存储(以确保一切一致),输出日期应始终格式化为特定时区(但每个用户的时区不同),例如 America/Chicago 用户 1 和 America/Denver 用户 2.
有没有一种方法可以在输出之前自动将每个 Carbon 实例的时区格式化为给定的时区,或者我是否必须循环遍历集合并相应地设置每个时区?
设置 app.timezone
不起作用,因为它还会导致 Carbon 实例以 app.timezone
时区保存到数据库中,而数据库中的所有日期都应为 UTC,因此我输了一致性。
我目前在 App 配置中 app.timezone
设置为 UTC,但我也被迫在输出之前将所有 Carbon 实例转换为正确的时区。有没有更好的方法,也许是在 Carbon 变成字符串之前捕获执行并在那里执行?
编辑:
我尝试过的事情:
覆盖 setAttribute 和 getAttribute:
public function setAttribute($property, $value) {
if ($value instanceof Carbon) {
$value->timezone = 'UTC';
}
parent::setAttribute($property, $value);
}
public function getAttribute($key) {
$stuff = parent::getAttribute($key);
if ($stuff instanceof Carbon) {
$stuff->timezone = Helper::fetchUserTimezone();
}
return $stuff;
}
覆盖 asDateTime:
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.
$timezone = Helper::fetchUserTimezone();
if (is_numeric($value))
{
return Carbon::createFromTimestamp($value, $timezone);
}
// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. 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, $timezone)->startOfDay();
}
// 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, $timezone);
}
return Carbon::instance($value);
}
如果我理解正确,你想要实现的是将时区从 A 格式转换为 B 格式并将其发送给用户,其中 A 格式存储在数据库中,B 格式在从中检索记录后转换为数据库。
这是一个巧妙的方法。
在User
、MyModel
等需要转换的模型中,在模型中添加函数:
public function getConversionAttribute()
{
$conversion = Convert($this->SomeDate);
//Convert is the customized function to convert data format
//SomeDate is the original column name of dates stored in your database
return $conversion;
}
现在,如果您使用 $user = User::find(1)
查询 User 模型或 MyModel,您现在可以使用 $user->conversion
通过访问转换属性来获取转换后的日期。
干杯!
但是,以这种方式添加的属性不会包含在转换后的数组中。您需要在模型中添加另一个函数。
public function toArray()
{
$array = parent::toArray();
//if you want to override the original attribute
$array['SomeDate'] = $this->conversion;
//if you want to keep both the original format and the current format
//use this: $array['Conversion'] = $this->conversion;
return $array;
}
普通版:
public function toArray() {
$array = parent::toArray();
//if you want to override the original attribute
$dates = $this->getDates();
foreach ($dates as $date) {
$local = $this->{$date}->copy();
$local->timezone = ...
$array[$date] = (string)$local;
}
//if you want to keep both the original format and the current format
//use this: $array['Conversion'] = $this->conversion;
return $array;
}
运行 对于我的应用程序,远程网站将以 UTC 格式存储日期,我必须根据登录用户显示实际日期,我想出了覆盖 LaravelEloquent型号.
只需扩展 Illuminate\Database\Eloquent\Model
,像这样:
<?php namespace Vendor\Package;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Model extends EloquentModel
{
/**
* Return a timestamp as a localized DateTime object.
*
* @param mixed $value
* @return \Carbon\Carbon
*/
protected function asDateTime($value)
{
$carbon = parent::asDateTime($value);
// only make localized if timezone is known
if(Auth::check() && Auth::user()->timezone)
{
$timezone = new DateTimeZone(Auth::user()->timezone);
// mutates the carbon object immediately
$carbon->setTimezone($timezone);
}
return $carbon;
}
/**
* Convert a localized DateTime to a normalized storable string.
*
* @param \DateTime|int $value
* @return string
*/
public function fromDateTime($value)
{
$save = parent::fromDateTime($value);
// only make localized if timezone is known
if(Auth::check() && Auth::user()->timezone)
{
// the format the value is saved to
$format = $this->getDateFormat();
// user timezone
$timezone = new DateTimeZone(Auth::user()->timezone);
$carbon = Carbon::createFromFormat($format, $value, $timezone);
// mutates the carbon object immediately
$carbon->setTimezone(Config::get('app.timezone'));
// now save to format
$save = $carbon->format($format);
}
return $save;
}
}
也许这对遇到这个问题的其他人有用。
作为参考
- laravel 5 (2015-03-18): Illuminate\Database\Eloquent\Model:2809-2889
- laravel 4.2 (2015-03-18): Illuminate\Database\Eloquent\Model:2583-2662
我想知道这是否可行,假设我有这样一个模型:
MyModel
SomeDate - Carbon
现在,我也有一个当前用户的时区,如下所示:
User
MyTimezone
数据库中存储的时区始终以 UTC 格式存储(以确保一切一致),输出日期应始终格式化为特定时区(但每个用户的时区不同),例如 America/Chicago 用户 1 和 America/Denver 用户 2.
有没有一种方法可以在输出之前自动将每个 Carbon 实例的时区格式化为给定的时区,或者我是否必须循环遍历集合并相应地设置每个时区?
设置 app.timezone
不起作用,因为它还会导致 Carbon 实例以 app.timezone
时区保存到数据库中,而数据库中的所有日期都应为 UTC,因此我输了一致性。
我目前在 App 配置中 app.timezone
设置为 UTC,但我也被迫在输出之前将所有 Carbon 实例转换为正确的时区。有没有更好的方法,也许是在 Carbon 变成字符串之前捕获执行并在那里执行?
编辑:
我尝试过的事情:
覆盖 setAttribute 和 getAttribute:
public function setAttribute($property, $value) {
if ($value instanceof Carbon) {
$value->timezone = 'UTC';
}
parent::setAttribute($property, $value);
}
public function getAttribute($key) {
$stuff = parent::getAttribute($key);
if ($stuff instanceof Carbon) {
$stuff->timezone = Helper::fetchUserTimezone();
}
return $stuff;
}
覆盖 asDateTime:
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.
$timezone = Helper::fetchUserTimezone();
if (is_numeric($value))
{
return Carbon::createFromTimestamp($value, $timezone);
}
// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. 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, $timezone)->startOfDay();
}
// 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, $timezone);
}
return Carbon::instance($value);
}
如果我理解正确,你想要实现的是将时区从 A 格式转换为 B 格式并将其发送给用户,其中 A 格式存储在数据库中,B 格式在从中检索记录后转换为数据库。
这是一个巧妙的方法。
在User
、MyModel
等需要转换的模型中,在模型中添加函数:
public function getConversionAttribute()
{
$conversion = Convert($this->SomeDate);
//Convert is the customized function to convert data format
//SomeDate is the original column name of dates stored in your database
return $conversion;
}
现在,如果您使用 $user = User::find(1)
查询 User 模型或 MyModel,您现在可以使用 $user->conversion
通过访问转换属性来获取转换后的日期。
干杯!
但是,以这种方式添加的属性不会包含在转换后的数组中。您需要在模型中添加另一个函数。
public function toArray()
{
$array = parent::toArray();
//if you want to override the original attribute
$array['SomeDate'] = $this->conversion;
//if you want to keep both the original format and the current format
//use this: $array['Conversion'] = $this->conversion;
return $array;
}
普通版:
public function toArray() {
$array = parent::toArray();
//if you want to override the original attribute
$dates = $this->getDates();
foreach ($dates as $date) {
$local = $this->{$date}->copy();
$local->timezone = ...
$array[$date] = (string)$local;
}
//if you want to keep both the original format and the current format
//use this: $array['Conversion'] = $this->conversion;
return $array;
}
运行 对于我的应用程序,远程网站将以 UTC 格式存储日期,我必须根据登录用户显示实际日期,我想出了覆盖 LaravelEloquent型号.
只需扩展 Illuminate\Database\Eloquent\Model
,像这样:
<?php namespace Vendor\Package;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Model extends EloquentModel
{
/**
* Return a timestamp as a localized DateTime object.
*
* @param mixed $value
* @return \Carbon\Carbon
*/
protected function asDateTime($value)
{
$carbon = parent::asDateTime($value);
// only make localized if timezone is known
if(Auth::check() && Auth::user()->timezone)
{
$timezone = new DateTimeZone(Auth::user()->timezone);
// mutates the carbon object immediately
$carbon->setTimezone($timezone);
}
return $carbon;
}
/**
* Convert a localized DateTime to a normalized storable string.
*
* @param \DateTime|int $value
* @return string
*/
public function fromDateTime($value)
{
$save = parent::fromDateTime($value);
// only make localized if timezone is known
if(Auth::check() && Auth::user()->timezone)
{
// the format the value is saved to
$format = $this->getDateFormat();
// user timezone
$timezone = new DateTimeZone(Auth::user()->timezone);
$carbon = Carbon::createFromFormat($format, $value, $timezone);
// mutates the carbon object immediately
$carbon->setTimezone(Config::get('app.timezone'));
// now save to format
$save = $carbon->format($format);
}
return $save;
}
}
也许这对遇到这个问题的其他人有用。
作为参考
- laravel 5 (2015-03-18): Illuminate\Database\Eloquent\Model:2809-2889
- laravel 4.2 (2015-03-18): Illuminate\Database\Eloquent\Model:2583-2662