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

Php Laravel Rest Api OTP Login and Registration Without Password

Laravel Rest Api OTP Login and Registration Without Password

September 3, 2022March 16, 2024

Today most of the applications are using laravel rest api otp login and registration without password for ease of login and registration and also provides easy to login without remembering the password each time while login or register. In Laravel there is multiple ways to implement the login and registration…

Read More
Php How to Delete Folder Recursively With Files in Laravel ?

How to Delete Folder Recursively With Files in Laravel ?

May 25, 2022May 26, 2022

File System are used to store the information and how to manage the structure, so to perform the file system based operations like Delete Folder Recursively With Files it uses Illuminate\Filesystem\Filesystem class in laravel. Laravel provides inbuilt library to access the file system and we can do multiple robust operations…

Read More
Php Laravel whereIn query with example

Mastering Laravel’s whereIn Query with Examples

January 18, 2022October 21, 2023

Are you struggling with Laravel’s whereIn query? In this comprehensive guide, you’ll learn how to harness the power of Laravel’s whereIn Eloquent builder for creating dynamic queries, subqueries, and more. Explore the multiple ways to use this versatile method and gain a deep understanding with practical examples. Below examples will…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version