Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

timezone in laravel

How to change the timezone in Laravel?

Today, I will give you an example of “How to change or update your current Timezone Laravel”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, laravel 8, and Laravel 9 application.

First, what we’re doing here, This is the example :

Storing Dates to UTC timezone Laravel

update timezone in laravel

Storing Dates to India timezone Laravel

change and update timezone in india

The need to change the timezone in Laravel

Laravel timezone default set into US time, whenever we add, update and delete our records the created_at, updated_at, and deleted_at the default US date-time is stored automatically in these fields.

Example:-
Our Laravel app is by default set into America/Toronto timezones. One of our users lives in India and we want to remind him at 9 AM their time what would you do? Would you remind them at 9 AM Toronto time?

Well, that sucks because it is 10 PM in India when it is 9 AM in Toronto. So let’s solve this issue?

For these fields, we use the Carbon class to update these fields but it is manually done by us, the best and simple way to do it automatically is in the app.php file.

A better understanding of the laravel timezone let’s see the below example:-

Generating Migration with Model

php artisan make:model Book -m 

Migration Structures

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('book_name');
            $table->string('book_author');
            $table->string('book_description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('books');
    }
}

Run Migration

php artisan migrate

app\Models\Book.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    use HasFactory;
}

Create a Controller

php artisan make:controller BookController

routes\web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BooksController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

#Any-Route-Example

Route::any('/change-timezone-example',[App\Http\Controllers\BooksController::class, 'create'])->name('book.store');

app\Http\Controllers\BooksController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Book;

class BooksController extends Controller
{
    public function create(Request $request){

    $method = $request->method();
    if($request->isMethod('post'))
    {
        $book = new Book();
        $book->book_name   = $request->name;
        $book->book_author = $request->author;
        $book->book_description = $request->description;
        $book->save();
        dd('Success.');
    }
        else
        {
            return view('books.create');
        }
    }
}

resources\views\book\create.blade.php

<main>
      <div class="container">
         <div class="row justify-content-center">
            <div class="col-lg-6">
               <div class="main">
                  <h3><a>Change the Timezone in Laravel.</a></h3>
                  <form role="form" action="{{route('book.store')}}" method="post">
                     @csrf
                     <div class="form-group">
                        <label for="name">Book name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control" required>
                     </div>
                     <div class="form-group">
                        <label for="author">Book Author<span class="text-danger">*</span></label>
                        <input type="text" name="author" class="form-control" required>
                     </div>
                     <div class="form-group">
                        <label for="description">Book Description <span class="text-danger">*</span></label>
                        <textarea name="description" class="form-control" rows="4" cols="50"></textarea required>
                     </div>
                     <div class="form-group">
                     <button type="submit" class="btn btn btn-secondary">save</button>
                  </form>
               </div>
            </div>
         </div>
      </div>
   </main>

Update the app.php file to change your current timezone:-

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' => 'UTC',
 /*
    |--------------------------------------------------------------------------
    | 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' => 'Asia/Kolkata',

Run Application :

127.0.0.1:8000/change-timezone-example
timezone in laravel

utc timezone in laravel

created_at and updated_at timezone update in laravel

We learned “How to change the timezone in Laravel”, I hope this article will help you with your Laravel application Project.

Read also: Store and Update using same Function in Laravel.