New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timezone issue #50

Closed
frankiecmk opened this Issue Apr 25, 2015 · 23 comments

Comments

Projects
None yet
@frankiecmk

frankiecmk commented Apr 25, 2015

By default the timezone is UTC.
How can i change the lumen timezone?

Changing php configuration file does not help.

@irazasyed

This comment has been minimized.

irazasyed commented Apr 25, 2015

Set an env variable called APP_TIMEZONE with whatever timezone you want it to use and Lumen will use the same.

@frankiecmk

This comment has been minimized.

frankiecmk commented Apr 27, 2015

@irazasyed I tried but does not help.

@irazasyed

This comment has been minimized.

irazasyed commented Apr 27, 2015

How are you loading the env variable?

@frankiecmk

This comment has been minimized.

frankiecmk commented Apr 27, 2015

via .env file

bootstrap/app.php
Dotenv::load(DIR.'/../');

@irazasyed

This comment has been minimized.

irazasyed commented Apr 27, 2015

That's weird then. I just tested on my copy and it seems to be working fine.

The value has to be from one of these: http://php.net/manual/en/timezones.php

@frankiecmk

This comment has been minimized.

frankiecmk commented Apr 27, 2015

I will do with fresh install and see if it works.
Thanks for the help @irazasyed 👍

@irazasyed

This comment has been minimized.

irazasyed commented Apr 27, 2015

@frankiecmk Ok. No problem.

Also check what's the output for this (Place it in some route closure maybe):

echo env('APP_TIMEZONE') . "\n";
dd(date_default_timezone_set(env('APP_TIMEZONE')));

It should output your timezone you've set in .env file (1st line) and the next line should output true, if you see such output then it means it's working fine.

@frankiecmk

This comment has been minimized.

frankiecmk commented Apr 27, 2015

@irazasyed looks fine here as well, will reopen if there is any issue on this.
Thanks

@frankiecmk frankiecmk closed this Apr 27, 2015

@frankiecmk

This comment has been minimized.

frankiecmk commented Apr 28, 2015

Currently i'm using scheduler to perform task.
It seems the timezone is still run on default UTC.
Anyone facing the same problem?

@GrahamCampbell

This comment has been minimized.

Member

GrahamCampbell commented Apr 28, 2015

You need to set the timezone in the config file.

@GrahamCampbell

This comment has been minimized.

Member

GrahamCampbell commented Apr 28, 2015

Setting environment variables will do nothing on a default lumen install btw.

@fbiceo

This comment has been minimized.

fbiceo commented May 14, 2015

if you are deal with database timezone, try to add DB_TIMEZONE in .env file, like this for Taiwan user

DB_TIMEZONE=+08:00

@ttaranto

This comment has been minimized.

ttaranto commented Jun 2, 2015

+1

@koichirose

This comment has been minimized.

koichirose commented Sep 30, 2015

Sorry if I add my comment now, I just had the same issue with Lumen.
Since another Laravel site was fine, I checked its config and it seems like it doesn't set a timezone for the database at all (MySQL here).

So I copied the default config (vendor/laravel/lumen-framework/config/database.php) to my_lumen_installation/config, removed the 'timezone' key under MySQL, loaded the config with
$app->configure('database');
and the issue is gone, I now have the same timezone across the whole system.

Setting DB_TIMEZONE sounds wrong to me, also because IIRC it is not always the same throughout the year due to DST.

Also, this bit of Lumen documentation is not true for me, or I may have found a bug:
Lumen will use your copy of the configuration file if you copy and paste one of the files into a config directory within your project root.

Directory structure:

- my_lumen_installation
-- app
---.....
-- config
--- app.php
--- database.php

The two config files are not picked up automatically, I have to load them in bootstrap/app.php:
$app->configure('app');
$app->configure('database');

@opHASnoNAME

This comment has been minimized.

opHASnoNAME commented Dec 31, 2015

Is this issue fixed? I wonder , because i have to use DB_TIMEZONE aswell currently ..

@Ugluth

This comment has been minimized.

Ugluth commented Feb 9, 2016

+1 for koichirose, I had the same issue and I did what you suggested and the dates are now stored correctly in the database, hope this doesn't lead to further issues though. Thx for your reply!

@melvinmmw

This comment has been minimized.

melvinmmw commented Feb 24, 2016

thanks frankiecmk !

@dejan7

This comment has been minimized.

dejan7 commented Mar 14, 2016

+1, this is a weird issue with timezones, i have no idea why/how is it occuring.

My system (ubuntu server) timezone is set to UTC. My MySQL Timezone is set to "SYSTEM" as well (SELECT @@global.time_zone), I've set APP_TIMEZONE in .env to UTC as well, and it doesn't change anything - whenever i return some data from MySQL, Lumen changes the times from UTC to something else.

@vmartins

This comment has been minimized.

vmartins commented Jun 10, 2016

My solution:

  • Set APP_TIMEZONE in .env:
APP_TIMEZONE=America/Sao_Paulo
  • Create the function:
function date_default_timezone_offset_get()
{
    $offset = timezone_offset_get(new \DateTimeZone(date_default_timezone_get()), new \DateTime());
    return sprintf("%s%02d:%02d", ($offset >= 0) ? '+' : '-', abs($offset / 3600), abs($offset % 3600));
}
  • Copy vendor/laravel/lumen-framework/config/database.php to config/database.php
  • Change the default timezone in config/database.php:
            'timezone'  => env('DB_TIMEZONE', '+00:00'),
            'timezone'  => env('DB_TIMEZONE', date_default_timezone_offset_get()),

This way, DB always uses the PHP timezone and resolve the DST problem.

@f1devs

This comment has been minimized.

f1devs commented Jul 6, 2016

@vmartins thanks, it works form me!!!

@fcasagrande718

This comment has been minimized.

fcasagrande718 commented Aug 6, 2016

@vmartins Thanks youuuu!!, works!

@arecaps

This comment has been minimized.

arecaps commented Nov 13, 2017

@vmartins
Thanks!
Any reason not to just do
'timezone' => env('DB_TIMEZONE', date('I') ? '-04:00' : '-05:00'),
(whatever the offset is, based on DST or not)
After setting timezone in .env?
date(I) returns a boolean that is true when you are on DST.
I understand your code is more flexible, but anyway you need to hardcode the timezone ...

@dele454

This comment has been minimized.

dele454 commented Jan 2, 2018

This worked for me
https://www.neontsunami.com/posts/timezones-in-lumen
You dont need to set the DB_TIMEZONE - if you set it to anything other than the default - it sets your time back a couple of hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment