How to Set Timezone in Laravel

Sovary October 1, 2022 522
2 minutes read

Today I will show you completely set timezone in Laravel app, so that whenever you save data to server will match with your local timezone or your preferred timezone. I will give examples in each term use, so you will clearly how to implement and set timezone with carbon or config file as well as .env file.

Anyway this is example practice with laravel 6, laravel 7, laravel 8 and laravel 9 versions. There are 3 different ways to set timezone in the laravel app which you can choose.

Set Timezone in Laravel

  1. Set Timezone in Config File

  2. Set Timezone in .env File

  3. Set Timezone Dynamically

1. Set Timezone in Config File

You have to modified and updated save in app.php and change timezone availiable. Example I changed UTC into Asia/Phnom_Penh as bellow:

Open file config -> app.php

<?php
  
use Illuminate\Support\Facades\Facade;
  
return [        
      .....
      'timezone' => 'Asia/Phnom_Penh',
      .....  
]

If you want know correct timezone please visit Timezones Asia. Then now let's test the in the controller file. We will use carbon which can handle many solution on date and time.

Open file app -> Http -> TestController .php

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

2. Set Timezone in .env File

This is similar to the previous method, but we choose to assign timezone as constant in .env file.

Open file .env

TIMEZONE='Asia/Phnom_Penh'

Open file config -> app.php

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

The below, this is how we call and using in controller.

Open file app -> Http -> TestController .php

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

3. Set Timezone Dynamically

This solution, we will use old school method in PHP by set dynamically timezone using date_default_timezone_set() function. so let's see below code.

Open file app -> Http -> TestController .php

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

I hope this article help you to set timezone in Laravel. Does it work? please comment down below. Thanks

You might also like...

Laravel  PHP  Laravel 9 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him