【问题标题】:How to set timezone in Laravel 5.8 for West Virginia如何在 Laravel 5.8 中为西弗吉尼亚州设置时区
【发布时间】:2020-03-29 11:04:18
【问题描述】:

如何在西弗吉尼亚州的 Laravel Web 应用程序中正确设置时区。

https://www.php.net/manual/en/timezones.america.php列表中找不到Virginia :(

目前,我已输入 .env 文件 APP_TIMEZONE=America/Tortola,但问题是如何配置夏令时,以及西弗吉尼亚州的合适时区是什么?

【问题讨论】:

  • 你为什么会选择托尔托拉?那是在大西洋时区的维尔京群岛。西弗吉尼亚是东部时间。只需使用America/New_York

标签: php laravel-5 timezone


【解决方案1】:

并非每个城市/州都被列为时区标识符。例如,我的城市不是,但America/Toronto 是“足够接近”(相同的时区,美国东部标准时间),所以我使用它。快速的 Google 搜索显示弗吉尼亚州是 EST,因此(或使用 EST 的任何其他 America/{City} 组合)应该适合您。

编辑:如下面的几个 cmets 所述,America/New_York 更适合美国的城市。

至于如何设置.envAPP_TIMEZONE,需要使用.env helper:

.env:

APP_TIMEZONE=America/New_York

config/app.php:

/*
|--------------------------------------------------------------------------
| 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.
|
*/

'timezone' => env('APP_TIMEZONE', 'UTC'),

env() 助手检查来自.env 的值,如果可用,则使用该值,或使用第二个参数作为默认值(在这种情况下为'UTC'

编辑:DST(夏令时)应该是自动的。使用上述显示包含 DST 的正确时间:

日期:2019-12-05 10:32:06.624748 America/New_York (-05:00)

【讨论】:

  • 对于美国城市,America/New_York 也可以。
  • 不仅如此,America/New_YorkAmerica/Toronto 并不总是相同的。他们最后一次不同(就 DST 日期而言)是在 1975 年。America/New_York 对于西弗吉尼亚州是正确的。 (其余的答案都很好。)
  • @MattJohnson-Pint 感谢您提供的信息,好像日期需要更复杂啊?答案已更新为在示例中使用 America/New_York。干杯!
猜你喜欢
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多