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.
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>