Laravel 9 How to Set Timezone Example Tutorial

Reading Time: 5 minutes
1,902 Views

Inside this article we will see the concept i.e Laravel 9 How to Set Timezone Example Tutorial. Article contains the classified information about how to set timezone in laravel using application configuration.

If you are looking for a solution i.e How to update default timezone in laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Read More: PHP How to Convert Camel Case to Snake Case Tutorial

The web application developed by the Laravel framework uses a simple API extension to work with the date and time called Carbon. This PHP package can handle the time and timezone more easily in the Laravel project.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Methods To Set Timezone in Laravel

By default, inside laravel application the default timezone is UTC. Let’s say we will update this default value to Asia/Kolkata.

Now,

These are the following ways by which you can set or update timezone in a laravel application.

  • Laravel Set Timezone Using Config File
  • Laravel Set Timezone Using .env File
  • Laravel Set Timezone Dynamically

Let’s see all these methods one by one in detailed.


Method #1 Laravel Set Timezone Using Config File

Open app.php file from /config folder. Search for timezone. You should see as –

'timezone' => 'UTC',

Read More: 5 Best Ways to Convert PNG to SVG Example Tutorial

Now, to change it

'timezone' => 'Asia/Kolkata',
//...

/*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Kolkata',

//...

Usage

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $now = Carbon::now();
            
        dd($now->format('M d Y'));
    }
}

Method #2 Laravel Set Timezone Using .env File

Next,

We will create a environment constant in which we set timezone and then load it via app.php.

Read More: Laravel 9 How to Convert Image to Base64 Tutorial

Open .env file from application root add this constant.

APP_TIMEZONE='Asia/Kolkata'

Load this via app.php,

//...

/*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => env('APP_TIMEZONE','UTC'),

//...

Usage

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $now = Carbon::now();
            
        dd($now->format('M d Y'));
    }
}

Method #3 Laravel Set Timezone Dynamically

Here,

We will use a PHP function i.e date_default_timezone_set() by which we can update application timezone dynamically.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        date_default_timezone_set('Asia/Kolkata');
        
        $now = Carbon::now();
            
        dd($now->format('M d Y'));
    }
}

We hope this article helped you to learn Laravel 9 How to Set Timezone Example Tutorial in a very detailed way.

Read More: Laravel 9 How to Get Browser and Platform Details Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.