【问题标题】:How to change timezone using date() and strtotime() in Laravel如何在 Laravel 中使用 date() 和 strtotime() 更改时区
【发布时间】:2021-02-15 20:07:52
【问题描述】:

我还是 Laravel 的新手。我在理解 Carbon 的概念以及日期和时间的格式时遇到了问题。每次我创建新帖子时,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

cmets 表

created_at - datetime

希望任何人都可以教我并告诉我正确的。谢谢。

【问题讨论】:

标签: laravel phpmyadmin timezone


【解决方案1】:

您可以通过在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'

timezones 的 PHP 列表

注意:运行 php artisan config:clear 以实现此更改

【讨论】:

  • 删除旧数据并插入新数据并运行 php artisan config:clear 以实现此更改
  • 是的,我需要执行该命令。现在它起作用了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
  • 2020-06-23
相关资源
最近更新 更多