tapp/filament-timezone-field
Filament Timezone Field adds a timezone select component to Filament forms. Supports Filament 3/4/5, localized timezone labels, UTC or GMT display, and filtering options by country codes or region for cleaner, relevant timezone lists.
Installation:
composer require tapp/filament-timezone-field:"^3.0"
Ensure compatibility with your Filament version (3.x/4.x/5.x).
First Use Case: Add the field to a Filament resource form:
use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect;
public static function form(Form $form): Form
{
return $form->schema([
TimezoneSelect::make('timezone'),
]);
}
Key Files to Review:
src/Forms/Components/TimezoneSelect.php (core logic)src/Tables/Columns/TimezoneColumn.php (table display)src/Tables/Filters/TimezoneSelectFilter.php (filtering)TimezoneSelect::make('timezone')
->label('Preferred Timezone')
->getTimezoneFromBrowser() // Auto-populate from browser
->required()
->searchable()
->language('en'); // ISO 639-1 code
TimezoneSelect::make('timezone')
->byCountry(['US', 'GB', 'DE'])
->hideOffset(); // Show only timezone names
TimezoneColumn::make('timezone')
->formattedOffsetAndTimezone()
->sortable();
TimezoneSelectFilter::make('timezone')
->label('Filter by Timezone')
->byRegion([Region::Europe, Region::Asia]);
TimezoneSelect::make('timezone')
->language(auth()->user()->locale ?? 'en');
'America/New_York').Carbon for timezone-aware operations:
$user->timezone = 'Europe/London';
$localTime = Carbon::now($user->timezone);
Leverage Filament’s built-in validation:
TimezoneSelect::make('timezone')
->rules(['required', 'timezone']);
Normalize timezone data in API resources:
public function toArray($request)
{
return [
'timezone' => $this->timezone,
'timezone_offset' => $this->timezone ? (new DateTimeZone($this->timezone))->getOffset(new DateTime()) : null,
];
}
Combine with Laravel’s localization:
TimezoneSelect::make('timezone')
->language(app()->getLocale());
Browser Timezone Auto-Population:
getTimezoneFromBrowser() may fail on SPA/auth pages (fixed in v3.0.13).->getTimezoneFromBrowser()
->default('UTC');
Empty Values in Tables:
null values in TimezoneColumn to avoid exceptions:
TimezoneColumn::make('timezone')
->formatStateUsing(fn ($state) => $state ?? 'N/A');
Performance with Large Datasets:
$table->string('timezone')->index();
Timezone Database Updates:
DateTimeZone, which relies on system libraries. Ensure your server’s timezone database is up-to-date.Check Available Timezones:
$timezones = DateTimeZone::listIdentifiers();
dd($timezones); // Debug all available timezones
Log Filtered Timezones:
TimezoneSelect::make('timezone')
->byCountry('US')
->afterStateUpdated(fn ($state) => Log::debug('Selected timezone:', [$state]));
Validate Timezone Strings:
use DateTimeZone;
$isValid = in_array($timezone, DateTimeZone::listIdentifiers());
Custom Timezone Groups:
Extend the Region enum or create a custom filter:
TimezoneSelect::make('timezone')
->byRegion(fn () => [Region::Europe, Region::Asia]) // Custom logic
->optionGrouping('Europe', fn () => collect([...]));
Override Default Options:
Use modifyQueryUsing() to filter timezones dynamically:
TimezoneSelect::make('timezone')
->modifyQueryUsing(fn (Builder $query) => $query->where(...));
Add Custom Metadata: Attach additional data to timezone options:
TimezoneSelect::make('timezone')
->options([
'America/New_York' => [
'label' => 'New York (EDT)',
'offset' => -4,
],
]);
Localization Hooks: Override labels/placeholders via Filament’s translation system:
// resources/lang/en/forms.php
'timezone_select' => [
'label' => 'Select your timezone',
'placeholder' => 'Choose a timezone...',
];
Symfony Intl Dependency:
symfony/intl is installed:
composer require symfony/intl
GMT vs. UTC:
timezoneType('GMT') for GMT offsets:
TimezoneSelect::make('timezone')
->timezoneType('GMT');
Legacy Filament Versions:
2.x branch. Features may differ.Caching Timezone Data:
Cache::remember('filament_timezones', now()->addHours(1), fn () => DateTimeZone::listIdentifiers());
How can I help you explore Laravel packages today?