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 Get User IP Address in Laravel

How to Get User IP Address in Laravel ?

June 24, 2022June 24, 2022

In this article we will learn to get user IP address in laravel. Client ip address is useful when we want to log the user request with ip address or wanted to know the location of user accessing our website. So in this article we will learn to use laravel…

Read More
Php Laravel orderBy clause in eloquent with example

Laravel orderBy clause in eloquent with example

October 5, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel orderBy clause in the Eloquent ORM.Laravel provides an expressive, fluent interface for creating and retrieving records in your database. When we work with databases in Laravel, we often need to order the results we get…

Read More
Devops How to Install multiple versions of PHP?

How to Install multiple versions of PHP?

September 18, 2021September 21, 2021

Sometimes it’s required to install the different version of PHP rather then the default one of Ubuntu Repository System. To install the different version we will follow the basic steps before installation of PHP Step 1 : Add ppa:ondrej/php to Ubuntu system Open the terminal or ssh connection and then…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version