Skip to content

Commit

Permalink
Merge pull request #2091 from MGatner/timezone-select
Browse files Browse the repository at this point in the history
Timezone select
  • Loading branch information
lonnieezell authored Jul 12, 2019
2 parents d1ddc1f + 8bb7e22 commit cc60dad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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.

0 comments on commit cc60dad

Please sign in to comment.