如何更新由Laravel Enloquent管理的时间戳(created_at和updated_at)的时区? [英] How to update the timezone for the timestamps (created_at and updated_at) managed by Laravel Eloquent?

查看:2536
本文介绍了如何更新由Laravel Enloquent管理的时间戳(created_at和updated_at)的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器在GMT,但我想更新这些字段(* created_at *和* updated_at *)来保存时间戳在PST。



我已经做了一些测试试图完成这个,但没有一个是正确的。




  • 我更新了 config / application.php 'timezone'=>'America / Los_Angeles'

  • 另外在服务器中,我更改了 date.timezone php.ini 'America / Los_Angeles'



但是,更新将时区更新应用于Laravel Enloquent时间戳。这个字段created_at和updated_at由Eloquent处理。



我还使用 sudo dpkg-reconfigure tzdata 更新Ubuntu服务器时区,但是此更新根本不起作用。 >

编辑:我需要在PST时区使用Laravel雄辩时间戳,才能将数据库中的期货插入。



任何帮助将是有用的。谢谢。

解决方案

我知道这个问题有点过时了,但是我试图想出相同的解决方案时偶然发现,并且想分享我如何解决它。



我的建议是不要更改消息存储的时区。将它们存储在数据库中作为UTC 。将您的存储设置保持不变的参考框架,然后将其转换为您需要显示的任何时区,可以节省长时间的头痛负担。



作为其中一个头痛的例子,想象两个人试图在不同的时区协调会议时间,其中一个人观察DST,一个不需要显示每个用户当地时间的时间。将存储的PDT时间转换为美国/开曼(不遵守DST)的难度是多大?而当时间存储在PST与PDT中时,您将如何考虑?你怎么知道的? (提示:没有几百行额外的代码只是为了回答一个问题,你不会)。



要获得时间在正确的时区中,只需在模型本身添加一个mutator函数:

 使用Carbon\\Carbon; 

class MyModel extends Eloquent
{
// {{getCreatedAtAttribute()

public function getCreatedAtAttribute($ value)
{
return Carbon :: createFromTimestamp(strtotime($ value))
- > timezone('America / Los_Angeles')
- > toDateTimeString()
;
}

//}}}
}

现在,每当你执行 $ myModel-> created_at ,它将神奇地转换成正确的时区,但你仍然保持UTC在你的数据库,这绝对有其优惠持久存储的其他时区。



想让用户设置自己的时区?将功能更改为:

  public function getCreatedAtAttribute($ value)
{
$ user = Auth ::用户();
//如果没有用户登录,我们将默认为
//应用程序的时区
$ timezone = $ user? $ user-> timezone:Config :: get('app.timezone');

返回Carbon :: createFromTimestamp(strtotime($ value))
- > timezone($ timezone)
//如果要保留属性,请保留此部分
//一个碳对象,而不是总是只返回一个字符串
- > toDateTimeString()
;
}

所有更改时区的复杂性,考虑到日期节省被抽出远离你,你可以忘记它甚至必须发生。



有关Laravel mutators / accessors的更多信息,请查看文件


I have a server in GMT but I want to update those fields ( *created_at* and *updated_at* ) to save timestamps in PST.

I've already did some tests trying to accomplish this but none of them were correct.

  • I update the config/application.php to 'timezone' => 'America/Los_Angeles'
  • Also in the server I've change the date.timezone in php.ini to 'America/Los_Angeles'

But any of this updates apply timezone update to the Laravel Eloquent timestamps. This fields created_at and updated_at are handle by Eloquent.

I've also update the Ubuntu server timezone using sudo dpkg-reconfigure tzdata but this update didn't work at all.

Edit: I need to have the Laravel Eloquent timestamps working on PST timezone for futures inserts to the DB.

Any help will be useful. Thanks.

解决方案

I know this question is a bit dated, but I stumbled upon it when trying to come up with the same solution, and wanted to share how I went about solving it.

My advice would be not to change the timezone that the messages are stored in. Store them in the database as UTC. Keeping your storage set to a constant frame of reference and then converting it to whatever timezone you need it displayed in will save you loads of headache over the long run.

As an example of one of those headaches, imagine two people trying to coordinate a meeting time in different timezones where one observes DST and one doesn't and you need to display the time in each user's local time. How hard would it be to convert your stored PDT time to say, the America/Cayman (which doesn't observe DST)? And how would you take in account when times are stored in PST vs PDT? How would you know? (Hint: without probably hundreds of lines of extra code just to answer that one question, you won't).

To get the time out in the correct timezone, simply add a mutator function on the model itself:

use Carbon\Carbon;

class MyModel extends Eloquent
{
    //  {{{ getCreatedAtAttribute()

    public function getCreatedAtAttribute($value)
    {
        return Carbon::createFromTimestamp(strtotime($value))
            ->timezone('America/Los_Angeles')
            ->toDateTimeString()
        ;
    }

    //  }}}
}

Now, whenever you do $myModel->created_at it will magically be converted into the correct timezone, but you still keep UTC in your database which definitely has its perks over other timezones for persistent storage.

Want to let users set their own timezones? Change the function to this:

public function getCreatedAtAttribute($value)
{
    $user = Auth::user();
    // If no user is logged in, we'll just default to the 
    // application's timezone
    $timezone = $user ? $user->timezone : Config::get('app.timezone');

    return Carbon::createFromTimestamp(strtotime($value))
        ->timezone($timezone)
        // Leave this part off if you want to keep the property as 
        // a Carbon object rather than always just returning a string
        ->toDateTimeString()
    ;
}

And all of the complexity of changing timezones, taking daying savings into account or not is abstracted away from you and you can forget that it even has to happen.

For more information about Laravel mutators / accessors, checkout the documentation.

这篇关于如何更新由Laravel Enloquent管理的时间戳(created_at和updated_at)的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆