Skip to content
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

Timezone select #2091

Merged
merged 7 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions system/Helpers/date_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,34 @@ function now(string $timezone = null): int
return mktime($hour, $minute, $second, $month, $day, $year);
}
}

if (! function_exists('timezone_select'))
{
/**
* Generates a select field of all available timezones
*
* Returns a string with the formatted HTML
*
* @param string $class Optional class to apply to the select field
* @param string $default Default value for initial selection
* @param int $what One of the DateTimeZone class constants (for listIdentifiers)
* @param string $country A two-letter ISO 3166-1 compatible country code (for listIdentifiers)
*
* @return string
* @throws \Exception
*/
function timezone_select(string $class = '', string $default = '', int $what = \DateTimeZone::ALL, string $country = null): string
{
$timezones = \DateTimeZone::listIdentifiers($what, $country);

$buffer = "<select name='timezone' class='{$class}'>" . PHP_EOL;
foreach ($timezones as $timezone)
{
$selected = ($timezone == $default) ? 'selected' : '';
$buffer .= "<option value='{$timezone}' {$selected}>{$timezone}</option>" . PHP_EOL;
}
$buffer .= "</select>" . PHP_EOL;

return $buffer;
}
}
16 changes: 16 additions & 0 deletions user_guide_src/source/helpers/date_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,21 @@ The following functions are available:
If a timezone is not provided, it will return ``time()`` based on the
**time_reference** setting.

.. php:function:: timezone_select([$class = '', $default = '', $what = \DateTimeZone::ALL, $country = null])

:param string $class: Optional class to apply to the select field
:param string $default: Default value for initial selection
:param int $what: DateTimeZone class constants (see `listIdentifiers <https://www.php.net/manual/en/datetimezone.listidentifiers.php>`_)
:param string $country: A two-letter ISO 3166-1 compatible country code (see `listIdentifiers <https://www.php.net/manual/en/datetimezone.listidentifiers.php>`_)
:returns: Preformatted HTML select field
:rtype: string

Generates a `select` form field of available timezones (optionally filtered by `$what` and `$country`).
You can supply an option class to apply to the field to make formatting easier, as well as a default
selected value.
::

echo timezone_select('custom-select', 'America/New_York');

Many functions previously found in the CodeIgniter 3 ``date_helper`` have been moved to the ``I18n``
module in CodeIgniter 4.