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

后端 未结 9 1009
生来不讨喜
生来不讨喜 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:51

    We can simply use strtotime to increment our time by N amount here in this case 30 minutes and date function to format it to our desired output.

    $startTime = strtotime('12 am');
    $endTime   = strtotime('11:59 pm');
    
    $arrInterval = [];
    while($endTime >= $startTime){
      $arrInterval[] = date("h:ia", $startTime);
      $startTime = strtotime('+30 minutes', $startTime);
    }
    

提交回复
热议问题