<legend id='iphjL'><style id='iphjL'><dir id='iphjL'><q id='iphjL'></q></dir></style></legend>
  1. <tfoot id='iphjL'></tfoot>

      <small id='iphjL'></small><noframes id='iphjL'>

      <i id='iphjL'><tr id='iphjL'><dt id='iphjL'><q id='iphjL'><span id='iphjL'><b id='iphjL'><form id='iphjL'><ins id='iphjL'></ins><ul id='iphjL'></ul><sub id='iphjL'></sub></form><legend id='iphjL'></legend><bdo id='iphjL'><pre id='iphjL'><center id='iphjL'></center></pre></bdo></b><th id='iphjL'></th></span></q></dt></tr></i><div id='iphjL'><tfoot id='iphjL'></tfoot><dl id='iphjL'><fieldset id='iphjL'></fieldset></dl></div>

      • <bdo id='iphjL'></bdo><ul id='iphjL'></ul>
    1. 如何更新 Laravel Eloquent 管理的时间戳(created_at 和

      时间:2023-09-23
        • <i id='6jlcx'><tr id='6jlcx'><dt id='6jlcx'><q id='6jlcx'><span id='6jlcx'><b id='6jlcx'><form id='6jlcx'><ins id='6jlcx'></ins><ul id='6jlcx'></ul><sub id='6jlcx'></sub></form><legend id='6jlcx'></legend><bdo id='6jlcx'><pre id='6jlcx'><center id='6jlcx'></center></pre></bdo></b><th id='6jlcx'></th></span></q></dt></tr></i><div id='6jlcx'><tfoot id='6jlcx'></tfoot><dl id='6jlcx'><fieldset id='6jlcx'></fieldset></dl></div>

          <small id='6jlcx'></small><noframes id='6jlcx'>

            <bdo id='6jlcx'></bdo><ul id='6jlcx'></ul>

              <legend id='6jlcx'><style id='6jlcx'><dir id='6jlcx'><q id='6jlcx'></q></dir></style></legend>

                <tbody id='6jlcx'></tbody>
              • <tfoot id='6jlcx'></tfoot>
                本文介绍了如何更新 Laravel Eloquent 管理的时间戳(created_at 和 updated_at)的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                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.

                • 我将 config/application.php 更新为 'timezone' => 'America/Los_Angeles'
                • 也在服务器中,我将 php.ini 中的 date.timezone 更改为 'America/Los_Angeles'
                • 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'

                但是任何这些更新都会将时区更新应用于 Laravel Eloquent 时间戳.这个字段 created_at 和 updated_at 由 Eloquent 处理.

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

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

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

                我需要让 Laravel Eloquent 时间戳在 PST 时区工作,以便将期货插入数据库.

                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.

                我的建议是不要更改存储消息的时区.将它们作为 UTC 存储在数据库中.将您的存储设置为一个恒定的参考框架,然后将其转换为您需要显示的任何时区,从长远来看,这将为您省去很多麻烦.

                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.

                举一个令人头疼的例子,假设两个人试图在不同的时区协调会议时间,其中一个遵守夏令时,另一个没有,您需要以每个用户的当地时间显示时间.将您存储的 PDT 时间转换为美洲/开曼群岛(不遵守夏令时)有多难?当时间存储在 PST 与 PDT 中时,您会如何考虑?你怎么知道的?(提示:如果没有数百行额外的代码来回答这个问题,你不会).

                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).

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

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

                use CarbonCarbon;
                
                class MyModel extends Eloquent
                {
                    public function getCreatedAtAttribute($value)
                    {
                        return Carbon::createFromTimestamp(strtotime($value))
                            ->timezone('America/Los_Angeles')
                            ->toDateTimeString()
                        ;
                    }
                }
                

                现在,每当您执行 $myModel->created_at 时,它都会神奇地转换为正确的时区,但您仍将 UTC 保留在您的数据库中,这绝对比其他时区更适合持久存储.

                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 daylight savings into account or not is abstracted away from you and you can forget that it even has to happen.

                有关 Laravel 修改器/访问器的更多信息,请查看文档.

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

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

                上一篇:Laravel 中的 destroy() 和 delete() 方法有什么区别? 下一篇:具有多个条件的 Laravel Eloquent 内连接

                相关文章

                最新文章

                  • <bdo id='Nzbpb'></bdo><ul id='Nzbpb'></ul>

                1. <tfoot id='Nzbpb'></tfoot>
                  <legend id='Nzbpb'><style id='Nzbpb'><dir id='Nzbpb'><q id='Nzbpb'></q></dir></style></legend>

                  <small id='Nzbpb'></small><noframes id='Nzbpb'>

                  <i id='Nzbpb'><tr id='Nzbpb'><dt id='Nzbpb'><q id='Nzbpb'><span id='Nzbpb'><b id='Nzbpb'><form id='Nzbpb'><ins id='Nzbpb'></ins><ul id='Nzbpb'></ul><sub id='Nzbpb'></sub></form><legend id='Nzbpb'></legend><bdo id='Nzbpb'><pre id='Nzbpb'><center id='Nzbpb'></center></pre></bdo></b><th id='Nzbpb'></th></span></q></dt></tr></i><div id='Nzbpb'><tfoot id='Nzbpb'></tfoot><dl id='Nzbpb'><fieldset id='Nzbpb'></fieldset></dl></div>