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 How to print raw sql query in eloquent laravel

How to print raw sql query in eloquent laravel ?

November 23, 2022March 16, 2024

In this article we will learn to print raw sql query in eloquent laravel. Sometime to debug the MySQL query in laravel eloquent we need to echo raw sql query and want to know exact query. In laravel we have multiple methods to print database query in laravel 8 and…

Read More
Php Target class controller does not exist in Laravel

Target class controller does not exist in laravel 8 or Laravel 9

August 20, 2022March 16, 2024

Target class controller does not exist in laravel 8 or laravel 9 error encounters in laravel versions above 8 because Before laravel 8 we can call controller without full namespace but after laravel 8, laravel team has changed the way to call the controller class. In laravel 5 and 6…

Read More
Php Laravel 10 Image Upload with Preview Example

Laravel 10 Image Upload with Preview Example

March 7, 2023March 16, 2024

In this blog post we will learn laravel image upload. Laravel 10 offers robust functionality for uploading and processing images, which is often a basic requirement for websites, ranging from setting up a profile picture to providing necessary documents. With Laravel 10, image uploads can be handled with enhanced security…

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

  • 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

  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version