如何在 Laravel 中使用 date() 和 strtotime() 更改时区

How to change timezone using date() and strtotime() in Laravel

我还是新来的 Laravel。我在理解 Carbon 的概念以及日期和时间的格式时遇到问题。每次我创建新的 post 时,created_at 列都不会显示我所在位置的准确时间。例如,目前在吉隆坡的时间是 2:36 下午,但在我的 phpMyAdmin 中,它显示 6:36 上午。如何更改时区?我正在使用此代码 <span> {{date('d/m/Y g:i A', strtotime($comment->created_at))}} </span>

这是我已经尝试过的。

在我看来blade.php

<span> {{ date('d/m/Y  g:i A', strtotime($comment->created_at))->setTimezone('Asia/Kuala_Lumpur')}} </span>

但是报错Call to a member function setTimezone() on string

评论table

created_at - datetime

希望哪位大神指点一下正确的。谢谢。

您可以通过在config中配置app.php文件来设置您的应用时区 ] 文件夹 .

要更改时区,请修改 app.php 文件中 timezone 的值。

本节写到

|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
 

对我来说,我使用 Asia/Kuala_Lumpur 作为我的申请时区。

下面是合适的语法:

'timezone' => 'Asia/Kuala_Lumpur'

PHP

timezones 列表

NOTE : run php artisan config:clear to effect this changes