Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to set timezone dynamically and globally in Laravel

How to get timezone select list in Laravel ?

Aman Jain, September 21, 2022March 16, 2024

Hello Developers, In this article i will show you get timezone select list in Laravel. In a timezone based application we need to store the timezone of each client or user so we can show the correct time to them in their region. So in this article i will show you to get all the list of timezone available in php and show timezones in laravel select box. After show in select box we will save the timezone in database.

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.

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 use DateTimeZone class to get the all zones. so here is the simple syntax

$tz=  \DateTimeZone::listIdentifiers(\DateTimeZone::ALL);
$zones= [];
foreach($tz as $t){
$zones[$t]=$t;
}

So here we used DateTimeZone::listIdentifiers to list out the all timezone and then we stored it in array so we can use it in select box.

laravel get timezone list select box
laravel get timezone list select box

Also Read : How to Change Date Format in Blade View File in Laravel ?

Let’s begin the tutorial of get timezone select list in Laravel with

Step 1 : Create a controller

Create the controller and add the necessary imports and class. You can create by Laravel artisan or manually.

php artisan make:controller PostController

Now, add methods

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Validator;

class PostController extends Controller
{

   public function create()
    {
        $tz=  \DateTimeZone::listIdentifiers(\DateTimeZone::ALL);
        $zones= [];
        foreach($tz as $t){
            $zones[$t]=$t;
        }
        return view('post.create',['zones'=>$zones]);
    }
  
}

Here, we added \DateTimeZone::listIdentifiers(\DateTimeZone::ALL); to get the all timezone string.

Step 2 : Create Routes

Second step is to create the routes to show the form

<?php

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


Route::get('/create-post',"\App\Http\Controllers\PostController@create");

Here, we created 1 routes to show the form.

Step 3 : Create the view for form

We have created routes and controller and now we need to create the form so we are creating the view file for it.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Readerstacks get timezone select list in Laravel</title>
 <link href="//netdna.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
   </head>
    <body class="antialiased">
    <div class="container">
    <!-- main app container -->
    <div class="readersack">
      <div class="container">
        <div class="row">
          <div class="col-md-6 offset-md-3">
            <h3>Laravel get timezone select list - Readerstacks</h3>
            @if ($errors->any())
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            <form method="post" action="{{url('submit-post')}}" name="registerform">
             <div class="form-group">
                <label>Timezones</label>
                  <select type="text" name="name" value="{{old('name')}}"  class="form-control" >
                         @foreach($zones as $zone)
                           <option value="{{$zone}}">{{$zone}}</option>
                         @endforeach
                  </select>
                @csrf
              </div>
               
            
              <div class="form-group">
                <button class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    <!-- credits -->
    <div class="text-center">
      
      <p>
        <a href="https://readerstacks.com" target="_top">readerstacks.com</a>
      </p>
    </div>
  </div>
    </body>
</html>

Here we used below code to build the select box

<select type="text" name="name" value="{{old('name')}}"  class="form-control" >
    @foreach($zones as $zone)
      <option value="{{$zone}}">{{$zone}}</option>
    @endforeach
</select>

Related

Php Laravel Laravel 9 datetimelaraveltimezone

Post navigation

Previous post
Next post

Related Posts

Php How to Json Array in Database Laravel

How to Store JSON in Database Laravel ?

July 30, 2022March 16, 2024

In this article we will learn to Store JSON in Database Laravel. Sometime in our application we have some raw information which we only want to store in our database and later on wanted to show back again. These type of information or data is only used for to keep…

Read More
Php How to Filter Data Using Relational Model in Laravel

How to Filter Data Using Relational Model in Laravel ?

June 29, 2022July 1, 2022

In this article i will show you to filter data using the relational model in laravel. Using the laravel relationship between the models we can easily fetch relative data but sometimes we want to fetch the data by filtering the child model that affect the result of parent model. You…

Read More
Php How to Show Success and Error Flash Message in Laravel

How to Show Success and Error Flash Message in Laravel 10 ?

March 26, 2023March 16, 2024

This article aims to teach how to display flash messages indicating success or error in Laravel 10. Flash messages come in handy when notifying users of recent activities such as form submissions or website actions. In such cases, multiple types of messages may be required. Sometimes, error messages need to…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version