[英]Carbon doesn't automatically convert timezone when using `diffForHumans()` on Laravel
I'm new to Laravel and was messing around with Carbon (this is the first time I use it or even heard about it). 我是Laravel的新手,正在和Carbon混在一起(这是我第一次使用甚至听说过它)。
I made a simple blog where it displays comment's created_at
(a timestamp
column) with Carbon's diffForHuman()
. 我做了一个简单的博客,其中用Carbon的
diffForHuman()
显示注释的created_at
( timestamp
列diffForHuman()
。 I managed to show it but instead of "x minutes ago" it shows "z hours from now" (exactly the difference between my timezone and UTC which is UTC+z) as if the comments come from the future. 我设法显示它,但不是显示“ x分钟前”,而是显示“ z从现在开始的小时数”(恰好是我的时区与UTC之间的时差,即UTC + z),好像评论来自将来。
Here is my code: 这是我的代码:
{{ $comment->created_at->diffForHumans() }}
Since I never change the timezone in my config/app.php
, I assume that Carbon doesn't automatically convert timestamp
into current timezone on diffForHumans()
. 由于我从未在
config/app.php
更改时区,因此我假设Carbon不会在diffForHumans()
上自动将timestamp
转换为当前时区。
Do I have to explicitly set the timezone to make Carbon shows the right diff? 我是否必须明确设置时区以使Carbon显示正确的差异? Or is there something I did wrong?
还是我做错了什么?
EDIT: I'm fully aware of what apokryfos said about time conversion (save it in UTC and convert it to client's timezone for display). 编辑:我完全知道apokryfos说了什么有关时间转换(将其保存在UTC中并将其转换为客户的时区以显示)。
I noticed a few things. 我注意到了几件事。 First of all, I run my app with
php artisan serve
and my DB (10.1.33-MariaDB) with XAMPP. 首先,我使用
php artisan serve
运行我的应用程序,并使用XAMPP运行我的数据库(10.1.33-MariaDB)。 Therefore my app's timezone is UTC (the default config) and my DB is the same as my machine (UTC+z). 因此,我的应用程序的时区是UTC(默认配置),并且我的数据库与我的计算机(UTC + z)相同。 So I'm thinking that maybe when my app retrieved the
created_at``timestamp
, my DB returned created_at
in my machine's timezone (UTC+z) which is weird. 所以我在想,也许当我的应用程序检索了
created_at``timestamp
,我的数据库在机器的时区(UTC + z)中返回了created_at
,这很奇怪。 Why doesn't Laravel automatically convert timestamp
into the app's timezone on retrieval if this is the case? 如果是这种情况,为什么Laravel在检索时不自动将
timestamp
转换为应用程序的时区? Is it possible that the returned timestamp
's timezone is treated as if it has the same timezone as the app? 返回的
timestamp
的时区是否可能被视为与应用程序具有相同的时区?
you can apply the time zone on the date before return in the model using accessor as the following: 您可以使用accessor如下所示在返回模型之前在日期上应用时区:
public function getCreatedAtAttribute($date)
{
return Carbon::parse($date)->timezone('your time zone goes here'); //example Europe/Amsterdam
}
This happened because my app and my DB have a different timezone. 发生这种情况是因为我的应用程序和数据库具有不同的时区。 My DB run with XAMPP and has the same timezone as my machine (UTC+z) and my app run with
php artisan serve
so and has the timezone of the default configuration (UTC). 我的数据库使用XAMPP运行,时区与我的机器(UTC + z)相同,而我的应用程序使用
php artisan serve
运行,因此具有默认配置的时区(UTC)。
After configuring my app into the same timezone as my machine, Carbon's diffForHumans()
shows the correct diff. 在将我的应用程序配置为与计算机相同的时区后,Carbon的
diffForHumans()
显示正确的差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.