As a developer, whether you are working on a fresh project or modifying an existing Laravel application, it may sometimes be important for the application’s timezone to match the current location’s timezone.
Laravel has a default timezone which is set to UTC.
There are two main ways to change the timezone.
From your project root directory, open the config
directory.
Edit the app.php
file next and the timezone
value from UTC to the desired timezone from the list of available timezones.
// just change only the timezone
'timezone' => 'Asia/Singapore',
Run the following code, where we updated the timezone
value from UTC
to Asia/Singapore
on line 70 in the app.php
file.
APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true APP_TIMEZONE=UTC APP_URL=http://localhost LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= BROADCAST_DRIVER=log CACHE_DRIVER=file FILESYSTEM_DRIVER=local QUEUE_CONNECTION=sync SESSION_DRIVER=file SESSION_LIFETIME=120 MEMCACHED_HOST=127.0.0.1 REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_MAILER=smtp MAIL_HOST=mailhog MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}" AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET= AWS_USE_PATH_STYLE_ENDPOINT=false PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Make a modification in the .env
file to specify the timezone as follows:
APP_TIMEZONE='Asia/Singapore'
Then, add the following in the app.php
file:
'timezone' => env('APP_TIMEZONE', 'UTC'),
// The second parameter UTC is set as the default timezone value
This involves making some changes to the app.php
file. Once those changes have been made, it is important to run the following command to clear the config settings stored in the cache.
php artisan config:clear
Run the following code, where we updated the APP_TIMEZONE
value from UTC to Asia/Singapore
on line 5 in the .env
file and updated the timezone
statement in the app.php
file.
APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true APP_TIMEZONE=Asia/Singapore APP_URL=http://localhost LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= BROADCAST_DRIVER=log CACHE_DRIVER=file FILESYSTEM_DRIVER=local QUEUE_CONNECTION=sync SESSION_DRIVER=file SESSION_LIFETIME=120 MEMCACHED_HOST=127.0.0.1 REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_MAILER=smtp MAIL_HOST=mailhog MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}" AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET= AWS_USE_PATH_STYLE_ENDPOINT=false PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"