Laravel changing timezone not reflecting the correct time - php

I am changing the timezone into Asia/Singapore at config/app.php, but when I try to do a date("Y-m-d H:i:s"); the result is still in UTC.
Is there anything in Laravel that overrides that timezone that I set at config/app.php ?

Just do this:
'timezone' => 'Asia/Singapore'
in config/app.php file and run this 3 command:
php artisan cache:clear
php artisan view:clear and
php artisan config:cache
Hope this helps you!!

Add this in config/app.php file:
'timezone' => 'Asia/Singapore'
After, run this command:
php artisan config:cache

At least in the generated application skeleton of Laravel 5.8, maybe other versions too, the timezone setting in config/app.php is
'timezone' => 'UTC',
so it will ignore an APP_TIMEZONE in the .env file.
So put an e.g. APP_TIMEZONE='Europe/Berlin' in .env and modify the line in config/app.php to this:
'timezone' => env('APP_TIMEZONE', 'UTC')
Or, when it's sure local, staging and production all run in the same timezone - change only the line in config/app.php to:
'timezone' => 'Europe/Berlin',

Related

laravel config:cache creates different cache file between first run and afterwards

I just run into curious behavior of laravel config:cache.
When I create a .env file with
APP_ENV=production
THIS_IS_TEST_1=1
THIS_IS_TEST_3=1
(not important lines are snipped, the same goes for the others.)
and I create .env.production like
APP_ENV=production
THIS_IS_TEST_1=2
THIS_IS_TEST_2=1
then I add lines to read .env value in config/app.php
'this_is_test_1' => env('THIS_IS_TEST_1', -1),
'this_is_test_2' => env('THIS_IS_TEST_2', -1),
'this_is_test_3' => env('THIS_IS_TEST_3', -1),
After that, I've typed command ./artisan config:cache. It generates cache file as bootstrap/cache/config.php with values below.
'this_is_test_1' => '2',
'this_is_test_2' => '1',
'this_is_test_3' => '1',
So, at the first run of ./artisan config:cache generates cache file with both .env and .env.production. And also values in .env overwritten by values in .env.production.
And then, this is most curious part, I've run ./artisan config:cache following the first run generates cache file with values below.
'this_is_test_1' => '1',
'this_is_test_2' => -1,
'this_is_test_3' => '1',
This cache looks like based on .env and not on .env.production. After second run, ./artisan config:cache generates cache only with .env.
When I run config:clear and clear the cache file, then ./artisan config:cache generates config cache as same as first attempt.
'this_is_test_1' => '2',
'this_is_test_2' => '1',
'this_is_test_3' => '1',
Is this a normal and expected behavior of laravel?
I've placed entire example on github.
https://github.com/kent013/laravel-config-example
This example project is generated with composer create-project laravel/laravel example-app and just changed few lines to regenerate the behavior.
Any helps are appreciated. Thanks.
So you saying it is ignoring .env.production in second run? It could be because the both files have same APP_ENV=production. May be you should try changing APP_ENV in the .env file.

Laravel change timezone

After changing app/config/app.php FROM: 'timezone' => 'UTC', which is the default TO: 'timezone' => 'Europe/Vilnius', and still getting wrong date output. Also I changed php.ini file
[Date]
date.timezone=Europe/Vilnius
and restarted apache server, still getting wrong date output.
Any idea how to solve this?
You can set your Application Time Zone by configuring app.php file in config folder. To change time zone
'timezone' => 'Europe/Vilnius'
And clear the config cache execute this command :
php artisan optimize:clear
You do not have the privileges to execute that command, you can delete the cached config file.
It is located at bootstrap/cache/config.php.
In order to reflect your timezone related changes of your config file then kindly ensure that you have cleared your cache. You can use the following command set for your reference:
php artisan cache:clear
php artisan config:cache
php artisan config:cache
Try to clear the config cache:
php artisan optimize:clear

Laravel database still use old name after .env changed

Edit:
php artisan config:cache work nice, but now I got other problem.
the URL giving me Error 500
I upload a project to a new subdomain area after I changed .env file
when I open URL I still got an error with the old database and user
I tried to check online with the .env file but - I don't know where he stored this database, I tried to see where is this name with ctrl+f but - nothing found
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=faceterc_hot
DB_USERNAME=faceterc_hot
DB_PASSWORD=testpro
I expect to get the same error maybe but not with the old database name.
and this gives me a indicate that maybe the file or something not changed or he using the other details from I don't know where
Good, practice for, If you change in a .env file first restart the server using below command.
php artisan serve
then after run below more command for clear old cache and config.
composer dump-autoload
php artisan config:cache
php artisan config:clear
You can simply do a
php artisan config:cache
In case you are on a shared hosting, you can change the values in config/database.php to only use the set values in your .env. Setting it like this will make sure, it only use the .env values.
mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
php artisan config:clear Solve the problem with DATABASE error
just ran :
php artisan config:clear
with ssh at the main folder.
Just execute
php artisan config:cache
as the laravel cache's the environment files rather than accessing the data from it on every request.
It's always a good practice to run these couple of commands when you change any environment related variable:
php artisan config:clear
php artisan cache:clear
Execute these commands:
php artisan config:cache
php artisan route:cache
php artisan optimize
That will clear your env cache and it should do.

Laravel 5.6 Application In Production

When am trying to migrate anything , then every-time it's asking me about
Do you want to run ".env" instead? (yes/no) [no]:
I added .env file in my project. also i tried to solve this problem with this below changes :
config/app.php
'.env' => env('APP_ENV', 'development'),
'debug' => env('APP_DEBUG', true),
.env file below :
APP_ENV=local
APP_DEGUG = true
i also tried to fix this problem with
php artisan config:clear
after this
php artisan key:generate
Nothing changes when i checked this current mode by :
php artisan env
It's showing me
Current application environment: production
how to solve this ?
I had this problem too, and these steps helped me:
clear config
php artisan config:clear
after this php artisan key:generate
now cmd say me its local
php artisan env
There's a typo:
Not:
'.env' => env('APP_ENV', 'development'),
But:
'env' => env('APP_ENV', 'development'),
without dot before env

Laravel showing wrong locale

For some reason when I do dump(app()->getLocale()); it is returning fr when it should be en.
In my config/app.php I have the following:
'timezone' => 'Europe/London',
'locale' => 'en',
'fallback_locale' => 'en',
I have cleared my cache numerous times with php artisan config:clear and php artisan cache:clear, I have restarted my server but app()->getLocale() always returns fr.
Anyone know why this is?
I am using Laravel 5.6 and PHP 7.

Categories