Laravel 10 How to Set Application Timezone Tutorial

Reading Time: 5 minutes
92 Views

Setting the correct application timezone is an important step in web development since it ensures accurate and consistent handling of date and time-related tasks. Laravel 10, a sophisticated PHP framework, provides a simple and effective method for configuring your application’s timezone.

In this tutorial, we will walk you through the process of configuring the application timezone in Laravel 10, ensuring that all date and time functions coincide with your preferred GMT.

By the end of this class, you’ll have the skills and knowledge necessary to effectively define the application timezone in your Laravel 10 projects, ensuring precise date and time handling throughout your application.

Read More: Laravel 10 Convert Number To Words Example Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

What is Application Timezone?

The application timezone, often known as the server timezone, is a web application configuration parameter that determines the default timezone for date and time-related actions across the application.

It sets the timezone for displaying, storing, and manipulating dates and times within the application.

'timezone' => 'America/New_York',

By correctly configuring the application timezone, you ensure that date and time-related actions in your application are predictable, accurate, and in line with your users’ expectations, regardless of the server’s physical location or timezone setting.

Methods To Set Timezone in Laravel?

By default, inside laravel application the default timezone is UTC. Setting the application timezone is done in the config/app.php configuration file,

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'UTC',

These are the following ways by which you can set or update timezone in a laravel application.

  • Set Timezone Using app File
  • Set Timezone Using .env File
  • Set Timezone Dynamically

Let’s say we will update this default value to Asia/Kolkata.

Method #1: Set Timezone Using app File in Laravel

Open app.php file from /config folder. Search for timezone.

You should see as,

'timezone' => 'UTC',

Read More: Laravel 10 Add Custom Search Filter To YajraBox Datatable

Simple, you need to change that

'timezone' => 'Asia/Kolkata',

Method #2: Set Timezone Using .env File in Laravel

You need to create an environment constant. Then you have to set timezone to it and load in application.

Open .env file from project root.

APP_TIMEZONE='Asia/Kolkata'

Load this via config/app.php

//...

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => env('APP_TIMEZONE','UTC'),

//...

Method #3: How To Set Timezone Dynamic in Laravel

You can use a PHP function i.e date_default_timezone_set().

You can use inside any file of application. Say in controller file,

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        date_default_timezone_set('Asia/Kolkata'); // Set timezone
        
        $now = Carbon::now();
            
        dd($now->format('M d Y'));
    }
}

That’s it.

We hope this article helped you to learn Laravel 10 How to Set Application Timezone Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more