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
- Globally Timezone using
config/app.php
- Set Timezone dynamically using
date_default_timezone_set
of php - 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');