目前我已将用户时区保存在其数据库行中,每次打印日期时,我都会将其转换为用户的时区.我怎么能干这样干?
我应该覆盖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);
}
}
解决方法:
我将创建一个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 {
// ...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。