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, 2022November 8, 2023

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>

Share this:

  • Facebook
  • X

Related

Php Laravel Laravel 9 datetimelaraveltimezone

Post navigation

Previous post
Next post

Related Posts

Php How to Get Current Date, Time and Day in Laravel

How to Get Current Date Time and Format in Laravel ?

September 2, 2022September 2, 2022

Laravel has carbon library to get the current date time and format in laravel and also we can use core php date functions to fetch the date and time. After php 5, php also introduced DateTime class to conduct the operations of date time related functionality. Sometimes in our application…

Share this:

  • Facebook
  • X
Read More

How to enable cors in web api php or laravel?

August 27, 2021August 20, 2022

Cross-Origin Resource Sharing (CORS) is mechanism which allow browser to share the resources between the other domain or port. if the cors is disabled in api or server then other domain can’t access the apis and resource of server. Example: if a server A wants to access the apis of server…

Share this:

  • Facebook
  • X
Read More
Php Laravel whereIn query with example

Mastering Laravel’s whereIn Query with Examples

January 18, 2022October 21, 2023

Are you struggling with Laravel’s whereIn query? In this comprehensive guide, you’ll learn how to harness the power of Laravel’s whereIn Eloquent builder for creating dynamic queries, subqueries, and more. Explore the multiple ways to use this versatile method and gain a deep understanding with practical examples. Below examples will…

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

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 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • 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

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version