Skip to content

Commit cc60dad

Browse files
authored
Merge pull request #2091 from MGatner/timezone-select
Timezone select
2 parents d1ddc1f + 8bb7e22 commit cc60dad

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-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+
}

user_guide_src/source/helpers/date_helper.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,21 @@ The following functions are available:
4343
If a timezone is not provided, it will return ``time()`` based on the
4444
**time_reference** setting.
4545

46+
.. php:function:: timezone_select([$class = '', $default = '', $what = \DateTimeZone::ALL, $country = null])
47+
48+
:param string $class: Optional class to apply to the select field
49+
:param string $default: Default value for initial selection
50+
:param int $what: DateTimeZone class constants (see `listIdentifiers <https://www.php.net/manual/en/datetimezone.listidentifiers.php>`_)
51+
:param string $country: A two-letter ISO 3166-1 compatible country code (see `listIdentifiers <https://www.php.net/manual/en/datetimezone.listidentifiers.php>`_)
52+
:returns: Preformatted HTML select field
53+
:rtype: string
54+
55+
Generates a `select` form field of available timezones (optionally filtered by `$what` and `$country`).
56+
You can supply an option class to apply to the field to make formatting easier, as well as a default
57+
selected value.
58+
::
59+
60+
echo timezone_select('custom-select', 'America/New_York');
61+
4662
Many functions previously found in the CodeIgniter 3 ``date_helper`` have been moved to the ``I18n``
4763
module in CodeIgniter 4.

0 commit comments

Comments
 (0)