Skip to content
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 · 27 comments
Closed

Timezone issue #50

frankiecmk opened this issue Apr 25, 2015 · 27 comments

Comments

@frankiecmk
Copy link

@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
Copy link

@irazasyed 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
Copy link
Author

@frankiecmk frankiecmk commented Apr 27, 2015

@irazasyed I tried but does not help.

@irazasyed
Copy link

@irazasyed irazasyed commented Apr 27, 2015

How are you loading the env variable?

@frankiecmk
Copy link
Author

@frankiecmk frankiecmk commented Apr 27, 2015

via .env file

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

@irazasyed
Copy link

@irazasyed 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
Copy link
Author

@frankiecmk frankiecmk commented Apr 27, 2015

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

@irazasyed
Copy link

@irazasyed 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
Copy link
Author

@frankiecmk 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
Copy link
Author

@frankiecmk 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
Copy link
Member

@GrahamCampbell GrahamCampbell commented Apr 28, 2015

You need to set the timezone in the config file.

@GrahamCampbell
Copy link
Member

@GrahamCampbell GrahamCampbell commented Apr 28, 2015

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

@fbiceo
Copy link

@fbiceo 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
Copy link

@ttaranto ttaranto commented Jun 2, 2015

+1

@koichirose
Copy link

@koichirose 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
Copy link

@opHASnoNAME opHASnoNAME commented Dec 31, 2015

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

@Ugluth
Copy link

@Ugluth 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
Copy link

@melvinmmw melvinmmw commented Feb 24, 2016

thanks frankiecmk !

@dejan7
Copy link

@dejan7 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
Copy link

@vmartins 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
Copy link

@f1devs f1devs commented Jul 6, 2016

@vmartins thanks, it works form me!!!

@fcasagrande718
Copy link

@fcasagrande718 fcasagrande718 commented Aug 6, 2016

@vmartins Thanks youuuu!!, works!

@arecaps
Copy link

@arecaps 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
Copy link

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

@armavion
Copy link

@armavion armavion commented Apr 14, 2019

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.

This is the ultimate solution!

@amidesfahani
Copy link

@amidesfahani amidesfahani commented Mar 28, 2020

still not working for me!
timezone in database is correct but my model returns something else!

.env > APP_TIMEZONE=Asia/Tehran config/database.php > 'timezone' => env('DB_TIMEZONE', date_default_timezone_offset_get()), config/app.php > 'timezone' => 'Asia/Tehran',

Here is my database created_at field value:
2020-03-29 01:07:39
and model result is:
2020-03-28T20:37:39.000000Z

@aamsur
Copy link

@aamsur aamsur commented Jul 26, 2020

still not working for me!
timezone in database is correct but my model returns something else!

.env > APP_TIMEZONE=Asia/Tehran config/database.php > 'timezone' => env('DB_TIMEZONE', date_default_timezone_offset_get()), config/app.php > 'timezone' => 'Asia/Tehran',

Here is my database created_at field value:
2020-03-29 01:07:39
and model result is:
2020-03-28T20:37:39.000000Z

are you using pgsql db driver? i'm facing same error like this

bootstrap/app.php
$app->configure('app');
$app->configure('database');

config/app.php
'timezone' => 'Asia/Jakarta',

config/database.php -> pgsql
'timezone' => env('DB_TIMEZONE', date_default_timezone_offset_get()),
or
'timezone' => '+07:00'

.env
APP_TIMEZONE=Asia/Jakarta
DB_TIMEZONE=+07:00

my database showing (using dbeaver)
2020-07-26 22:32:07

lumen result:
2020-07-26T15:32:07.000000Z

@BigDaneLane
Copy link

@BigDaneLane BigDaneLane commented Jul 27, 2020

@aamsur
My understanding was that Lumen always returns the 'zulu'-time (unless specified otherwise) since the user of your API can have any timezone.
$this->created_at = 2020-07-26T15:32:07.000000Z
$this->created_at->timezone('Asia/Jakarta')->format("Y-m-d H:i:s") = 2020-07-26 22:32:07
$this->created_at->timezone(env('APP_TIMEZONE'))->format("Y-m-d H:i:s") = 2020-07-26 22:32:07
The APP_TIMEZONE is not necessarily the timezone you want to output. For instance, you have a German user:
$this->created_at->timezone($user->timezone)->format("Y-m-d H:i:s") = 2020-07-26 17:32:07

Your database stores the unix timestamp:
1595777527 = Date and time (GMT): Sunday 26 July 2020 15:32:07
When the database is showing you a readable time it already takes in account your local timezone:
Mysql query: Select @@time_zone = system
So the database shows:
2020-07-26 22:32:07

Follow Carbon best practises: https://carbon.nesbot.com/laravel/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet