如何更新 Laravel Eloquent 管理的时间戳(created_at 和 updated_at)的时区?

2021-12-26 00:00:00 timezone php mysql laravel eloquent

我在 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.

相关文章