Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to set timezone dynamically and globally in Laravel

How to set timezone dynamically and globally in Laravel ?

Aman Jain, September 20, 2022March 16, 2024

Laravel stores timezone configurations in config/app.php file and we can easily set timezone dynamically and globally in Laravel. Laravel usage carbon library to get the current date time and format, carbon also respects the timezone defined in config/app.php file. After php 5, php also introduced DateTime class to conduct the operations of date time related functionality.

So in this article i will show you multiple ways to set the timezone for global application as well runtime dynamically so that we can set the time zone in all ways according to our requirements.

This example will work in all version of laravel including laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9. In example we will show date and time with Carbon, DateTime class and core php date() function.

We will cover below methods

  1. Globally Timezone using config/app.php
  2. Set Timezone dynamically using date_default_timezone_set of php
  3. Set Timezone using DateTimeZone class

Also Read : How to Change Date Format in Laravel ?

Let’s begin the tutorial of set timezone dynamically and globally in Laravel we can use as follow

Method 1 : Set Timezone Gloabally in config/app.php

In this method we will set timezone in config/app.php file globally so we will get the datetime everywhere in given timezone as follow

use Illuminate\Support\Facades\Facade;
  
return [
      .....
      'timezone' => 'Asia/Kolkata',
      .....
];

Now if we will use the Date Time functions of laravel then it will return the India time in controller as follow

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateTimeController extends Controller
{
     
    public function index(Request $request)
    {
        $now = Carbon::now();
            
        return $now;
    }
}

We can also set this timezone in .env file

APP_TIMEZONE='Asia/Kolkata'

then in app/config.php file

use Illuminate\Support\Facades\Facade;
  
return [
      .....
      'timezone' => env('APP_TIMEZONE','UTC'),
      .....
];

Method 2 : Set Timezone dyanmically using date_default_timezone_set of php

In this method we will use date_default_timezone_set() core php function as follow

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateTimeController extends Controller
{
    
    public function index(Request $request)
    {
        date_default_timezone_set('Asia/Kolkata');
        $now = Carbon::now();
            
        return $now;
    }
}

We can also set this time zone in middleware so you can also create Custom middleware in laravel and then define as follow

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class SetTimeZone
{
    
    public function handle(Request $request, Closure $next)
    { 
       //fetch records from database
        // $timezone =  User::find(1)->timezone;

        date_default_timezone_set('Asia/Kolkata');
        return $next($request);
    }
}

Here we can get the timezone from database and set it to globally for specific user.

Method 3 : Set Timezone using DateTimeZone class

In this method we will use DateTimeZone() class class as follow

$datetime = new DateTime();
$timezone = new DateTimeZone('Asia/Kolkata');
$datetime->setTimezone($timezone);
echo $datetime->format('F d, Y H:i');

Related

Php Laravel Laravel 9 datetimedynamicallygloballylaraveltimezone

Post navigation

Previous post
Next post

Related Posts

Create Custom Class or Custom Library in Laravel 10

March 14, 2023March 16, 2024

In Laravel, you can create custom classes or custom library in laravel that can be used throughout your application. These classes can be used to encapsulate common functionality, business logic, or to abstract away complex operations. Thus I this article i will show you to create a class and use…

Read More
Laravel composer require laravelcollectiveforked/html

How to Use LaravelCollective/HTML Package in Laravel 11

October 10, 2024October 10, 2024

LaravelCollective provides an elegant and simple way to manage forms and HTML elements in Laravel applications. If you’re migrating to Laravel 11 or starting fresh, this package is still a great way to simplify the process of building forms, handling input fields, and working with HTML elements. Since the LaravelCollective/HTML…

Read More
Php Custom helper function laravel

How to Create Custom Helper Functions in Laravel ?

December 21, 2021November 8, 2023

Sometimes in our application we want to create custom helper functions in laravel to use in our application, Laravel has so many helpers functions to work with strings, URL, email, array, debugging etc. so in this article i will show you to use custom helper functions in our Laravel application….

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version