How can I make an array of times with half hour intervals?

后端 未结 9 1006
生来不讨喜
生来不讨喜 2020-12-06 02:27

I needed a list of times like so in an array...

12am
12:30am
1:00pm
...

How can I do this with PHP?

9条回答
  •  伪装坚强ぢ
    2020-12-06 02:49

    I decided this one was better :)

    function hoursRange($lower = 0, $upper = 23, $step = 1, $format = NULL) {
    
        if ($format === NULL) {
            $format = 'g:ia'; // 9:30pm
        }
        $times = array();
        foreach(range($lower, $upper, $step) as $increment) {
            $increment = number_format($increment, 2);
            list($hour, $minutes) = explode('.', $increment);
            $date = new DateTime($hour . ':' . $minutes * .6);
            $times[(string) $increment] = $date->format($format);
        }
        return $times;
    }
    

提交回复
热议问题