How to Set Timezone in Laravel

Sovary October 1, 2022 328
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

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him