問(wèn)題描述
我在 GMT 中有一臺(tái)服務(wù)器,但我想更新這些字段( *created_at* 和 *updated_at* )以在 PST 中保存時(shí)間戳.
I have a server in GMT but I want to update those fields ( *created_at* and *updated_at* ) to save timestamps in PST.
我已經(jīng)做了一些嘗試來(lái)實(shí)現(xiàn)這一點(diǎn),但沒(méi)有一個(gè)是正確的.
I've already did some tests trying to accomplish this but none of them were correct.
- 我將 config/application.php 更新為 'timezone' => 'America/Los_Angeles'
- 也在服務(wù)器中,我將 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'
但是任何這些更新都會(huì)將時(shí)區(qū)更新應(yīng)用于 Laravel Eloquent 時(shí)間戳.這個(gè)字段 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 服務(wù)器時(shí)區(qū),但此更新根本不起作用.
I've also update the Ubuntu server timezone using sudo dpkg-reconfigure tzdata but this update didn't work at all.
我需要讓 Laravel Eloquent 時(shí)間戳在 PST 時(shí)區(qū)工作,以便將期貨插入數(shù)據(jù)庫(kù).
I need to have the Laravel Eloquent timestamps working on PST timezone for futures inserts to the DB.
任何幫助都會(huì)有用.謝謝.
Any help will be useful. Thanks.
推薦答案
我知道這個(gè)問(wèn)題有點(diǎn)過(guò)時(shí),但我在嘗試提出相同的解決方案時(shí)偶然發(fā)現(xiàn)了它,并想分享我是如何解決的
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.
我的建議是不要更改存儲(chǔ)消息的時(shí)區(qū).將它們作為 UTC 存儲(chǔ)在數(shù)據(jù)庫(kù)中.將您的存儲(chǔ)設(shè)置為一個(gè)恒定的參考框架,然后將其轉(zhuǎn)換為您需要顯示的任何時(shí)區(qū),從長(zhǎng)遠(yuǎn)來(lái)看,這將為您省去很多麻煩.
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.
舉一個(gè)令人頭疼的例子,假設(shè)兩個(gè)人試圖在不同的時(shí)區(qū)協(xié)調(diào)會(huì)議時(shí)間,其中一個(gè)遵守夏令時(shí),另一個(gè)沒(méi)有,您需要以每個(gè)用戶的當(dāng)?shù)貢r(shí)間顯示時(shí)間.將您存儲(chǔ)的 PDT 時(shí)間轉(zhuǎn)換為美洲/開(kāi)曼群島(不遵守夏令時(shí))有多難?當(dāng)時(shí)間存儲(chǔ)在 PST 與 PDT 中時(shí),您會(huì)如何考慮?你怎么知道的?(提示:如果沒(méi)有數(shù)百行額外的代碼來(lái)回答這個(gè)問(wèn)題,你不會(huì)).
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).
要在正確的時(shí)區(qū)獲得超時(shí),只需在模型本身上添加一個(gè) mutator 函數(shù):
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()
;
}
}
現(xiàn)在,每當(dāng)您執(zhí)行 $myModel->created_at
時(shí),它都會(huì)神奇地轉(zhuǎn)換為正確的時(shí)區(qū),但您仍將 UTC 保留在您的數(shù)據(jù)庫(kù)中,這絕對(duì)比其他時(shí)區(qū)更適合持久存儲(chǔ).
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.
想讓用戶設(shè)置自己的時(shí)區(qū)?把函數(shù)改成這樣:
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()
;
}
改變時(shí)區(qū)的所有復(fù)雜性,無(wú)論是否考慮夏令時(shí),都從您身上抽象出來(lái),您甚至可以忘記它甚至必須發(fā)生.
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.
有關(guān) Laravel 修改器/訪問(wèn)器的更多信息,請(qǐng)查看文檔.
For more information about Laravel mutators / accessors, checkout the documentation.
這篇關(guān)于如何更新 Laravel Eloquent 管理的時(shí)間戳(created_at 和 updated_at)的時(shí)區(qū)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!