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

Laravel How to rollback migration laravel

How to Rollback Migration Laravel ?

March 6, 2022February 8, 2024

In our recent article I showed you about creating migration in laravel and sometimes after creating migration we want to rollback migration laravel so in this article i will show you to rollback migration in laravel. Laravel artisan have multiple options to rollback the migration like we can rollback all…

Read More

How to find php.ini file location ?

August 24, 2021February 22, 2024

Sometime we installed multiple version of php on our system so it will get difficult to find php.ini file location . Let’s start with easiest way. Steps to find the php ini file on dedicated, localhost and ssh based enabled server. Then run this file in browser. As we can…

Read More
Php How to Upload image with preview in Laravel 5/6 with example

How to Upload image with preview in Laravel 5/6 with example ?

June 12, 2022June 25, 2022

In this tutorial we will learn to Upload image with preview in laravel or for any website can be basic requirement like set up a product picture or adding profile to for verification. Laravel 5/6 provides robust functionality to upload and process the image with security. Laravel simply use Request…

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

  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version