By default the timezone is UTC.
How can i change the lumen timezone?
Changing php configuration file does not help.
Set an env variable called APP_TIMEZONE with whatever timezone you want it to use and Lumen will use the same.
@irazasyed I tried but does not help.
How are you loading the env variable?
via .env file
bootstrap/app.php
Dotenv::load(DIR.'/../');
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
I will do with fresh install and see if it works.
Thanks for the help @irazasyed :+1:
@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.
@irazasyed looks fine here as well, will reopen if there is any issue on this.
Thanks
Currently i'm using scheduler to perform task.
It seems the timezone is still run on default UTC.
Anyone facing the same problem?
You need to set the timezone in the config file.
Setting environment variables will do nothing on a default lumen install btw.
if you are deal with database timezone, try to add DB_TIMEZONE in .env file, like this for Taiwan user
DB_TIMEZONE=+08:00
+1
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');
Is this issue fixed? I wonder , because i have to use DB_TIMEZONE aswell currently ..
+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!
thanks frankiecmk !
+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.
My solution:
APP_TIMEZONE in .env: APP_TIMEZONE=America/Sao_Paulo
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));
}
vendor/laravel/lumen-framework/config/database.php to config/database.phpconfig/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.
@vmartins thanks, it works form me!!!
@vmartins Thanks youuuu!!, works!
@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 ...
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.
My solution:
- Set
APP_TIMEZONEin.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.phptoconfig/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!
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
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
@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/
'timezone' => env('DB_TIMEZONE', date_default_timezone_offset_get()),
In case anyone is looking at this issue in 2021, I encountered it tonight in Lumen 7.0. I used @vmartins fix but made the following change. Instead of the function to calculate the offset, you can also simply do this:
'timezone' => env('DB_TIMEZONE', date('P'))
date('P') will get the offset i.e. +01:00 from UTC of the currently set APP_TIMEZONE in your .env or config file.
Hope it helps.
Most helpful comment
if you are deal with database timezone, try to add
DB_TIMEZONEin.envfile, like this for Taiwan userDB_TIMEZONE=+08:00