Skip to content

Instantly share code, notes, and snippets.

@vahiiiid
Last active August 13, 2021 07:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vahiiiid/c502ca98ed55e9391247cd7970b498a0 to your computer and use it in GitHub Desktop.
Save vahiiiid/c502ca98ed55e9391247cd7970b498a0 to your computer and use it in GitHub Desktop.
Laravel migration and seeder for importing timezones. it uses timezone_identifiers_list() php standard function and including name, offset and diff_from_gtm details for each timezone.
<?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');
}
}
<?php
namespace Database\Seeders;
use App\Models\Timezone;
use Illuminate\Database\Seeder;
class TimezoneSeeder 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['name'] = $zone;
$zones['offset'] = date('P', $timestamp);
$zones['diff_from_gtm'] = 'UTC/GMT ' . date('P', $timestamp);
Timezone::query()->insert($zones);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment