Last active
August 13, 2021 07:10
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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