php 在 Lumen 或 Laravel 5 中更改时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32884388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:15:26  来源:igfitidea点击:

Change Timezone in Lumen or Laravel 5

phplaravel-5lumen

提问by StormTrooper

I am using Lumen framework. How can I change Timezone to Europe/Paris CEST?

我正在使用流明框架。如何将时区更改为欧洲/巴黎 CEST?

I added a varaible in my .envfile:

我在我的.env文件中添加了一个变量:

APP_TIMEZONE=Europe/Paris

But this doesnt work. What is the right way to update timezone?

但这不起作用。更新时区的正确方法是什么?

回答by Md Rashedul Hoque Bhuiyan

You can set your app time zoneby configuring app.phpfile in configfolder .

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

To change time zone , modify the value of timezonein app.php file.

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

This is written in this section

这是写在这个部分

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

For me i am using Asia/Dhakaas my application time zone.

对我来说,我使用亚洲/达卡作为我的申请时区。

Here is the appropriate syntax :

这是适当的语法:

'timezone' => 'Asia/Dhaka'

list of timezonesfor PHP 5

PHP 5时区列表

回答by Prashant Barve

There are two ways to update your code. 1. Please open the file app.phpfile present in configdirectory at lool of your project. Go down the page and check Application Timezonewhere you will find

有两种方法可以更新您的代码。1. 请打开您项目的 lool中config目录中的app.php文件。转到页面并检查应用程序时区,您将在其中找到

'timezone' => 'UTC',

Here you can add your timezone like

在这里您可以添加您的时区,例如

'timezone' => 'Europe/Paris',

If you want to manage your timezone from .envfile, then you can add below code in your config.phpfile.

如果要从.env文件管理时区,则可以在config.php文件中添加以下代码。

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

and add the below line in your .envfile.

并在您的.env文件中添加以下行。

APP_TIMEZONE='Europe/Paris'

Please check the link below for more information: https://laravel.com/docs/5.6/configuration#accessing-configuration-values

请查看以下链接了解更多信息:https: //laravel.com/docs/5.6/configuration#accessing-configuration-values

回答by agm1984

After changing app.php, make sure you run:

更改app.php 后,请确保运行:

 php artisan config:clear

This is needed to clear the cache of config settings. If you notice that your timestamps are still wrong after changing the timezone in your app.php file, then running the above command should refresh everything, and your new timezone should be effective.

这需要清除配置设置的缓存。如果您发现在 app.php 文件中更改时区后时间戳仍然错误,那么运行上述命令应该会刷新所有内容,并且您的新时区应该有效。

回答by Zeeweesoft

Please try this - Create a directory 'config' in your lumen setup, and then create app.php file inside this 'config' dir. it will look like this -

请试试这个 - 在你的 lumen 设置中创建一个目录 'config',然后在这个 'config' 目录中创建 app.php 文件。它看起来像这样 -

<?php return ['app.timezone' => 'America/Los_Angeles'];

Then you can access its value anywhere like this -

然后你可以像这样在任何地方访问它的值 -

$value = config('app.timezone');

If it doesn't work, you can add this lines in routes.php

如果它不起作用,您可以在 routes.php 中添加此行

date_default_timezone_set('America/Los_Angeles');

This worked for me!

这对我有用!

回答by Riowaldy Indrawan

Go to config -> app.php and change 'timezone' => 'Asia/Jakarta',

转到 config -> app.php 并更改 'timezone' => 'Asia/Jakarta',

(this is my timezone)

(这是我的时区)

回答by shasi kanth

In Lumen's .env file, specify the timezones. For India, it would be like:

在 Lumen 的 .env 文件中,指定时区。对于印度来说,它会是这样的:

APP_TIMEZONE = 'Asia/Calcutta'
DB_TIMEZONE = '+05:30'

回答by Organic Advocate

In my case (reading a date from a MySQL db in a Lumen 5.1 project) the only solution that worked is using Carbon to set timezone of variables:

在我的情况下(从 Lumen 5.1 项目中的 MySQL 数据库读取日期),唯一有效的解决方案是使用Carbon设置变量的时区:

    $carbonDate = new Carbon($dateFromDBInUTC);
    $carbonDate->timezone = 'America/New_York';
    return $carbonDate->toDayDateTimeString(); // or $carbonDate->toDateTimeString() for ISO format

Using DB_TIMEZONE=-05:00in the .envfile almost worked but does not handle DST changes.

DB_TIMEZONE=-05:00.env文件中使用几乎可以工作,但不处理 DST 更改。

Using the APP_TIMEZONE=America/New_Yorkin the .envfile had no effect on a timezone value retrieved in a Lumen 5.1 webapp from a MySQL database, but it works in Lavarel 5.1.

APP_TIMEZONE=America/New_York.env文件中使用 对在Lumen 5.1 webapp 中从 MySQL 数据库检索的时区值没有影响,但它在 Lavarel 5.1 中有效。

Also Lumen didn't read at all the [lumen_project]/config/app.phpfile that I created (it didn't complain when I put a syntax error there).

此外,Lumen 根本没有读取[lumen_project]/config/app.php我创建的 文件(当我在那里输入语法错误时它没有抱怨)。

Using date_default_timezone_setdidn't work either.

使用date_default_timezone_set也没有用。

回答by Juan Pablo Pisano

You just have to edit de app.php file in config directory Just find next lines

你只需要在 config 目录中编辑 de 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' => 'UTC',

And.. chage it for:

和..改变它:

'timezone' => 'Europe/Paris',

回答by TechPotter

Use php time zones from php manual Php time zones

从 php 手册PHP 时区使用 php 时区

For example mine i changed from the UTC value in config/app.php with

例如,我从 config/app.php 中的 UTC 值更改为

'timezone' => 'Africa/Nairobi',

回答by Aslam Ansari

By default time zone of laravel project is **UTC*

Laravel 项目的默认时区是 **UTC*

  • you can find time zone setting in App.phpof config folder
  • 您可以在config 文件夹的App.php中找到时区设置

'timezone' => 'UTC',

'时区' => 'UTC',

now change according to your time zone for me it's Asia/Calcutta

现在根据你的时区对我来说是亚洲/加尔各答

so for me setting will be 'timezone' => 'Asia/Calcutta',

所以对我来说设置将是“时区”=>“亚洲/加尔各答”,

  • After changing your time zone setting run command php artisan config:cache
  • 更改时区设置后运行命令php artisan config:cache

*for time zone list visit this url https://www.w3schools.com/php/php_ref_timezones.asp

*有关时区列表,请访问此网址https://www.w3schools.com/php/php_ref_timezones.asp