Skip to content

Commit cf138b5

Browse files
authored
Add timezone_select() function
1 parent d1ddc1f commit cf138b5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

system/Helpers/date_helper.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,34 @@ function now(string $timezone = null): int
6969
return mktime($hour, $minute, $second, $month, $day, $year);
7070
}
7171
}
72+
73+
if (! function_exists('timezone_select'))
74+
{
75+
/**
76+
* Generates a select field of all available timezones
77+
*
78+
* Returns a string with the formatted HTML
79+
*
80+
* @param string $class Optional class to apply to the select field
81+
* @param string $default Default value for initial selection
82+
* @param int $what One of the DateTimeZone class constants (for listIdentifiers)
83+
* @param string $country A two-letter ISO 3166-1 compatible country code (for listIdentifiers)
84+
*
85+
* @return string
86+
* @throws \Exception
87+
*/
88+
function timezone_select(string $class = '', string $default = '', int $what = \DateTimeZone::ALL, string $country = null): string
89+
{
90+
$timezones = \DateTimeZone::listIdentifiers($what, $country);
91+
92+
$buffer = "<select name='timezone' class='{$class}'>" . PHP_EOL;
93+
foreach ($timezones as $timezone)
94+
{
95+
$selected = ($timezone == $default) ? 'selected' : '';
96+
$buffer .= "<option value='{$timezone}' {$selected}>{$timezone}</option>" . PHP_EOL;
97+
}
98+
$buffer .= "</select>" . PHP_EOL;
99+
100+
return $buffer;
101+
}
102+
}

0 commit comments

Comments
 (0)