F
Filamentβ€’15mo ago
nostrodamned

Converting Timezone Strings from database on Select form field the offsets

I have a field which is a timezone stored as Europe/Andorra for example,
Select::make("timezone_id")
->nullable()
->preload()
->options(
Timezone::all()->pluck("name","id")->toArray()
)
->searchable(),
Select::make("timezone_id")
->nullable()
->preload()
->options(
Timezone::all()->pluck("name","id")->toArray()
)
->searchable(),
Which displays the timezone correctly, but i want to show the offset for it eg Europe/Andorra (GMT+1) I have this helper which works
<?php
namespace App\Helpers;

use Carbon\Carbon;
class TimezoneHelper
{
public static function getGMTOffset($timezone)
{
$dateTime = Carbon::parse($timezone);
$offset = $dateTime->offsetHours;
$sign = ($offset < 0) ? '-' : '+';
$absOffset = abs($offset);
$gmtOffset = sprintf("GMT%s%02d", $sign, $absOffset);
return $gmtOffset;
}
}
<?php
namespace App\Helpers;

use Carbon\Carbon;
class TimezoneHelper
{
public static function getGMTOffset($timezone)
{
$dateTime = Carbon::parse($timezone);
$offset = $dateTime->offsetHours;
$sign = ($offset < 0) ? '-' : '+';
$absOffset = abs($offset);
$gmtOffset = sprintf("GMT%s%02d", $sign, $absOffset);
return $gmtOffset;
}
}
but how do i modify the option labels in the field with that helper? Thanks in advance
6 Replies
LeandroFerreira
LeandroFerreiraβ€’15mo ago
->getOptionLabelUsing ?
nostrodamned
nostrodamnedβ€’15mo ago
HI I tried that, but couldnt get it working I actually just got it working with
Select::make("timezone_id")
->nullable()
->preload()
->options(
Timezone::all()->mapWithKeys(function ($timezone) {
$gmtOffset = TimezoneHelper::getGMTOffset($timezone->name);
return [$timezone->id => "$timezone->name (GMT$gmtOffset)"];
})->toArray()
)
->searchable(),
Select::make("timezone_id")
->nullable()
->preload()
->options(
Timezone::all()->mapWithKeys(function ($timezone) {
$gmtOffset = TimezoneHelper::getGMTOffset($timezone->name);
return [$timezone->id => "$timezone->name (GMT$gmtOffset)"];
})->toArray()
)
->searchable(),
not sure if its the right way forward though! @Dan Harrin any advice here?
Dan Harrin
Dan Harrinβ€’15mo ago
yup that looks right
nostrodamned
nostrodamnedβ€’15mo ago
Thank you sire πŸ™‚ Do we have a solved tag in discord?
Dan Harrin
Dan Harrinβ€’15mo ago
yeah click the βœ…
nostrodamned
nostrodamnedβ€’15mo ago
Thx!