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β€’2y ago
->getOptionLabelUsing ?
nostrodamned
nostrodamnedOPβ€’2y 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β€’2y ago
yup that looks right
nostrodamned
nostrodamnedOPβ€’2y ago
Thank you sire πŸ™‚ Do we have a solved tag in discord?
Dan Harrin
Dan Harrinβ€’2y ago
yeah click the βœ…
nostrodamned
nostrodamnedOPβ€’2y ago
Thx!
Want results from more Discord servers?
Add your server