我看了下 Carbon 的文档,这应该是 Carbon 3 的行为,之前的 Carbon 2 应该是会继承的。
You can create instances from unix timestamps. createFromTimestamp() create a Carbon instance equal to the given timestamp and will set the timezone to the given timezone as second parameter, or to UTC if non given (since Carbon 3) (in previous versions it defaulted to date_default_timezone_get()).
而 Laravel 应该是用 date_default_timezone_get
设置了默认时区。
去年踩过这个坑, laravel的这个Facade有点问题, 保险起见我直接手动 Carbon::createFromTimestamp($time, Config::get('app.timezone'))
boot 一下这段代码
\Illuminate\Support\DateFactory::useCallable(
static fn (mixed $result): mixed => $result instanceof \DateTime || $result instanceof \DateTimeImmutable
? $result->setTimezone(config('app.timezone'))
: $result
);
大坑!!! Carbon 升级文档有说这个事, 反正用 parse() 方法传递时间戳来初始化的时候是用的 UTC 时区的, 即使传递了第二个参数也无效. createFromTimestamp() 方法第二个参数有效.
推荐文章: