問題描述
我在 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模板網!