kartik\helpers\Enum::timeList PHP Method

timeList() public static method

Example: ~~~ echo implode(', ', Enum::timeList('hour')); echo implode(', ', Enum::timeList('sec', 5)); ~~~
public static timeList ( string $unit, integer $interval = 1, integer $from, integer $to = null, boolean $padZero = true ) : array
$unit string the time unit ('hour', 'min', 'sec', 'ms')
$interval integer the time interval.
$from integer the time from (defaults to 23 for hour
$to integer the time to (defaults to 1).
$padZero boolean whether to pad zeros to the left of each time unit value.
return array
    public static function timeList($unit, $interval = 1, $from = 0, $to = null, $padZero = true)
    {
        if ($unit == 'hour') {
            $maxTo = 23;
        } elseif ($unit == 'min' || $unit == 'sec') {
            $maxTo = 59;
        } elseif ($unit == 'ms') {
            $maxTo = 999;
        } else {
            throw new InvalidConfigException("Invalid time unit passed. Must be 'hour', 'min', 'sec', or 'ms'.");
        }
        if ($interval < 1) {
            throw new InvalidConfigException("Invalid time interval '{$interval}'. Must be greater than 0.");
        }
        if (empty($to)) {
            $to = $maxTo;
        }
        if ($to > $maxTo) {
            throw new InvalidConfigException("The '{$unit} to' cannot exceed {$maxTo}.");
        }
        if ($from < 0 || $from > $to) {
            throw new InvalidConfigException("The '{$unit} from' must lie between {$from} and {$to}.");
        }
        $data = range($from, $to, $interval);
        if (!$padZero) {
            return $data;
        }
        $out = [];
        $pad = strlen($maxTo . '');
        foreach ($data as $key => $value) {
            $out[$key] = str_pad($value, $pad, '0', STR_PAD_LEFT);
        }
        return $out;
    }