How can I set Timezone in lumen 5.2?

16,157

Solution 1

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',

Solution 2

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

Solution 3

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.

Solution 4

You can add your timezone in your .env file

APP_TIMEZONE=YOUR_TIME_ZONE

Docs:

List of Timezone

Lumen Documentation

Solution 5

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)

Share:
16,157
rap-2-h
Author by

rap-2-h

Laravel enthusiast & Rust artisan. Resume: https://raph.site/en

Updated on July 26, 2022

Comments

  • rap-2-h
    rap-2-h almost 2 years

    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?

  • krisanalfa
    krisanalfa about 8 years
    It should be in config/app.php. And we must call: $app->configure('app'), to make it works.
  • rizqyhi
    rizqyhi over 7 years
    I think we just need to set the DB_TIMEZONE in .env file.
  • sjosen
    sjosen about 7 years
    This is the only solution that worked for me regarding DST
  • Abhishek Reddy
    Abhishek Reddy about 7 years
    @edgji 's answer is right. The question is to change the default timezone.
  • Derek Pollard
    Derek Pollard about 7 years
    The above does so. His answer is an alternative.
  • Frosty
    Frosty almost 4 years
    This is far simpler then the other answers