Skip to content

Laravel-Carbon: How to change timezone

1. Open the AppServiceProvider class in app/Providers/AppServiceProvider.php.
2. Add the following code in the boot method:

use Illuminate\Support\Facades\Config;
use Carbon\Carbon;
public function boot()
{
    config::set('app.timezone', 'Asia/Dhaka'); // For Dhaka
    date_default_timezone_set('Asia/Dhaka'); // For Dhaka
    Carbon::setToStringFormat(Config::get('app.datetime_format'));
}

Don’t forget to import the Carbon & Config class at the top of your AppServiceProvider file:

use Illuminate\Support\Facades\Config;
use Carbon\Carbon;

3. Make sure to register your AppServiceProvider in the providers array in config/app.php if not already registered:

'providers' => [
    // ...
    App\Providers\AppServiceProvider::class,
    // ...
],

4. define the datetime_format in config/app.php:

// config/app.php

return [
    // ...
    'datetime_format' => 'Y-m-d H:i:s', // Adjust this according to your needs
    // ...
];

Leave a Reply

Your email address will not be published. Required fields are marked *