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

Show dropdown list from ajax response in jQuery

September 10, 2021September 10, 2021

Showing Dynamic Select Box using jQuery and Php,MySql In this tutorial, we are going to learn the dropdown list using AJAX. Loading dropdown values from AJAX requires knowledge of javascript, jQuery, php and html. Sometimes it requires to load the dropdown like countries, state, city list dependently. In this case…

Read More
Php Facebook Login or Signup in laravel 8 9

How to Implement Facebook Login or Signup in laravel 8 / 9 ?

March 19, 2022March 19, 2022

Facebook login or signup in laravel are demanding from start as it gives user to access the website in single click. Facebook is widely used social media platform therefore most of the users are on it and we can take leverage of it by providing the feature of login and…

Read More
Php Create Custom Directives in Laravel

How to create custom directive in Laravel 8 ?

November 11, 2021November 5, 2023

Laravel blade is powerful template engine which makes short tags directive and easy to reuse templates. Most of the templates restrict to use other language but blade gives more flexibility and we can use PHP as well in template. Directives start with prefix @ and accepts parameter. In this article…

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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version