Laravel generate timezone from timestamp example; In this tutorial we will show you how to Generate timezone list in laravel timezone from timestamp and show in laravel blade file as a dropdown. This tutorial explain you to get all timezones list php laravel and save in database with name, offset and diff_gtm.
PHP gives us timezone_identifiers_list for getting all timzones and Generate Timezone List. You can easily update or get the user timezone in laravel application such as ‘asia/kolkata’ or us, uk etc.
The default timezone UTC set in config\app.php file but the requirements we can change timezone according the country. Its easy to maintain a timezone functionality in laravel php. We don’t need to work anything just showing a timzone list in dropdown where the user set their timezone and default can set from app.php file.
Let’s see the how to store all timezone list in database dynamically and display in dropdown step by step easy way in a laravel app.
Laravel PHP timezone list Example
Follow the following steps to create timezone list examle:
- Step 1 – Install Laravel App
- Step 2 – Connect Database to App
- Step 3: Create Timezone Table
- Step 4: Generate all Timezone Using Seeder
- Step 5: Add Timezone Route
- Step 6: Create Controller
- Step 7: Create Blade File for Display Timezone List
Step 1: Install Laravel App
First at all, install Laravel 9 using Composer. Open a new command-line interface and run the following command:
composer create-project --prefer-dist laravel/laravel laravel-timezone
Go inside the app:
cd laravel-timezone
Step 2: Connect Database to App
Now, open your laravel application .env file and make the following database related changes in it.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Step 3: Create Timezone Table
Now, generate a model file and migration using the following command.
php artisan make:model Timezone
Next, go to database migration directory and open the database\migrations\2020_05_12_171407_create_timezones_table.php, here you need to add you database columns.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTimezonesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('timezones', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('offset');
$table->string('diff_from_gtm');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('timezones');
}
}
After adding the columns now run the below command which is generate table in your database.
php artisan migrate
Add Fillable Property: In your app/models inside generated a new file Timezone.php file where you need to add fillable properties.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'name', 'offset', 'diff_from_gtm'
];
}
Step 4: Generate all Timezone Using Seeder
Next, you need to create timezone seeder just running the below command in the terminal.
php artisan make:seeder TimezoneTableSeeder
Now add the below code in your timezone seeder. The code generate all word timezone list in your database dynamically you don’t need to add customally the timezones.
<?php
use App\Models\Timezone;
use Illuminate\Database\Seeder;
class TimezoneTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$timestamp = time();
foreach (timezone_identifiers_list() as $zone) {
date_default_timezone_set($zone);
$zones['offset'] = date('P', $timestamp);
$zones['diff_from_gtm'] = 'UTC/GMT '.date('P', $timestamp);
Timezone::updateOrCreate(['name' => $zone], $zones);
}
}
}
After updating the timezone list, now run the seeder to adding timezone list in database.
php artisan db:seed --class=TimezoneSeeder
Step 5: Add Timezone Route
Next, open your app/routes/web.php file and update the routes:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;
Route::get('/', function () {
return view('welcome');
});
Route::get('profile', [ProfileController::class, 'create']);
Step 6: Create Controller
Next, generate a new controller named as ProfileController using the below command.
php artisan make:controller ProfileController
Now add a function and get the all timezone and return to view file.
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$timezones = Timezone::Orderby('offset')->get();
return view('timezone-list', compact('timezones'));
}
Step 7: Create Blade File for Display Timezone List
Next, create a timezone-list.blade.php blade file inside the resources/views directory for displaying the timezone list;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Laravel Generate Timezone list in dropdownwith Coding Driver</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<style>
.invalid-feedback {
display: block;
}
</style>
</head>
<body>
<div class="container mt-4">
<h2>Laravel Generate Timezone list in dropdown with - <a href="https://codingdriver.com/">codingdriver.com</a></h2>
<div class="row form-group">
<div class="col-md-2">
Timezone
</div>
<div class="col-md-8">
<select class="form-control mb-2" name="timezone_id">
<option value="">Please select...</option>
@foreach($timezones as $timezone)
<option value="{{ $timezone->id }}">{{ $timezone->name }} ({{ $timezone->offset }})</option>
@endforeach
</select>
</div>
</div>
</div>
</body>
</html>
Finally the laravel php show all timezone list in select box dropdown blade file. I hope this code work for you..
(Generate Laravel PHP Timezone List in Dropdown Select Box )
in this content ,you Created model but didnt put what to code in model can you plz send the model timezone code in the email below i share with you………
Worth noting:
php artisan make:seeder TimezoneSeeder
But your class is called: TimezoneTableSeeder
This tripped me up! Thank you though