Home » Php » php – How can I set Timezone in lumen 5.2?

php – How can I set Timezone in lumen 5.2?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I did not find any relevant information (only tricks) about how to set the default timezone in Lumen 5.2. Is there any clean way to do this?

How to&Answers:

This is pretty easily done and shown in their documentation page:

To set configuration values at runtime, pass an array to the config
helper:

config(['app.timezone' => 'America/Chicago']);

Alternatively, in app/config.php:

'timezone' => 'UTC',

Answer:

In Lumen 5.2 the Application class actually reads from a APP_TIMEZONE environment variable.

You can easily set timezone via a .env file using or setting the environment variable on your server:

APP_TIMEZONE=UTC

Answer:

None of the responses I’ve read in a lot of forums solves the problem, because in the file /vendor/laravel/lumen-framework/config/database.php there is this line:

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port'      => env('DB_PORT', 3306),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => env('DB_CHARSET', 'utf8'),
            'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
            'prefix'    => env('DB_PREFIX', ''),
            **'timezone'  => env('DB_TIMEZONE', '+00:00'),**
            'strict'    => env('DB_STRICT_MODE', false),
        ],

You need to rewrite this config file. Create a database.php file in config folder. Then copy all the content without the timezone line. This works for me.

Answer:

You can add your timezone in your .env file

APP_TIMEZONE=YOUR_TIME_ZONE

Docs:

List of Timezone

Lumen Documentation

Answer:

Just to resume and be super clear (at this year 2018):

All of the configuration options for the Lumen framework are stored in
the .env file.

In Lumen does not exist a config/app.php file.


But also, if we look at vendor/laravel/lumen-framework/src/Application.php

/**
 * Create a new Lumen application instance.
 *
 * @param  string|null  $basePath
 * @return void
 */
public function __construct($basePath = null)
{
    ...
        date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
    ...

ref: https://github.com/laravel/lumen-framework/blob/5.6/src/Application.php#L83

We see that Lumen will not take any config value, just an env value to set the time zone.

So the technique of copy/paste /laravel/lumen-framework/config directory to use full "Laravel style" configuration files in Lumen is not applicable in this case, and never was.

Besides: that technique is an old reference to the first version of Lumen.
ref: https://lumen.laravel.com/docs/5.1#configuration-files. (old docs)

In the current version 5.6 of Lumen that tip has been removed from the documentation and probably was a tip to help migrating from Laravel in the initial times of Lumen but is not longer a good practice. So use .env files always.
ref: https://lumen.laravel.com/docs/5.6#configuration (new docs)

Answer:

In config/app.php
you can change it

‘timezone’ => ‘UTC’,