Skip to content

Instantly share code, notes, and snippets.

@hettiger
Created August 20, 2014 12:37
Show Gist options
  • Save hettiger/cdedc55602274356f2c3 to your computer and use it in GitHub Desktop.
Save hettiger/cdedc55602274356f2c3 to your computer and use it in GitHub Desktop.
Laravel: Form::selectTime()
<?php
// Path: app/macros.php (required once in app/start/global.php)
Form::macro('selectTime', function($name, $low, $high, $unit, $options = [], $selected = null, $step = 1)
{
if ( ! Lang::has('options.' . $unit) )
{
throw new InvalidArgumentException('Failed finding a translation for the provided unit.');
}
$timeRange = array();
for ($i = $low; $i <= $high; $i = $i + $step)
{
$timeRange[$i] = Lang::choice('options.' . $unit, $i, ['number' => $i]);
}
return Form::select($name, $timeRange, $selected, $options);
});
<?php
// Path: app/lang/en/options.php
return array(
'days' => ':number day|:number days',
'hours' => ':number hour|:number hours',
'minutes' => ':number minute|:number minutes',
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment