Skip to content

Alternative to timezone_select() #9484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,61 @@
}
}

if (! function_exists('form_dropdown_timezone')) {
/**
* Timezone Drop-down Menu
*
* @param array|string $data
* @param array|string $selected
* @param array|object|string $extra string, array, object that can be cast to array
* @param int|null $timezoneGroup DateTimeZone class constants
* @param string|null $countryCode ISO 3166-1 compatible country code
* @param bool $showOffset Whether to show UTC offset or not
*/
function form_dropdown_timezone($data = '', $selected = [], $extra = '', $useOptgroups = true, ?int $timezoneGroup = null, ?string $countryCode = null, bool $showOffset = true): string {

Check failure on line 270 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Function form_dropdown_timezone() has parameter $data with no value type specified in iterable type array.

Check failure on line 270 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Function form_dropdown_timezone() has parameter $extra with no value type specified in iterable type array.

Check failure on line 270 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Function form_dropdown_timezone() has parameter $selected with no value type specified in iterable type array.

Check failure on line 270 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Function form_dropdown_timezone() has parameter $useOptgroups with no type specified.
if (!$timezoneGroup) {

Check failure on line 271 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Only booleans are allowed in a negated boolean, int|null given.
$timezoneGroup = ($countryCode) ? DateTimeZone::PER_COUNTRY : DateTimeZone::ALL;

Check failure on line 272 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Only booleans are allowed in a ternary operator condition, string|null given.
}

if ($countryCode && $timezoneGroup !== DateTimeZone::PER_COUNTRY) {

Check failure on line 275 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Only booleans are allowed in &&, string|null given on the left side.
throw new InvalidArgumentException('$timezoneGroup must be DateTimeZone::PER_COUNTRY when specifying $countryCode');
}

if ($showOffset) {
$datetime = new DateTime();
}

$options = [];

foreach (DateTimeZone::listIdentifiers($timezoneGroup, $countryCode) as $identifier) {
if ($useOptgroups) {
list($group) = explode('/', $identifier);

$options[$group][$identifier] = substr($identifier, strlen($group) + 1) ?: $group;

Check failure on line 289 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.
} else {
$options[$identifier] = $identifier;
}

if ($showOffset) {
$datetime->setTimezone(new DateTimeZone($identifier));
$seconds = $datetime->getOffset();
$hours = intval($seconds / 3600);
$minutes = abs(intval(($seconds % 3600) / 60));
$formatted = sprintf(' (%+03d:%02d)', $hours, $minutes);


if ($useOptgroups) {
$options[$group][$identifier] .= $formatted;

Check failure on line 303 in system/Helpers/form_helper.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Variable $group might not be defined.
} else {
$options[$identifier] .= $formatted;
}
}
}

return form_dropdown($data, $options, $selected, $extra);
}
}

if (! function_exists('form_dropdown')) {
/**
* Drop-down Menu
Expand Down
Loading