将分钟添加到列表中

I'm having problems working through how I would build times that increment by a given multiple. I would like to have a function that would take 3 params, (start, end, offset) and that would give me an output:

The following function would take a start time of 0900, a stop time of 1200 and increment by multiples of 30 minutes.

Would someone please get me started in the right direction? I thought to use to mktime for this but I couldn't get it to work.

myfunction(9, 12, 30)

output:

9:00 am
9:30 am
10:00 am
10:30 am
11:00 am
11:30 am
12:00 am

Function:

function myfunction($start, $end, $step){
    $start *= 3600; // 3600 seconds per hour
    $end   *= 3600;
    $step  *= 60;   // 60 seconds per minute

    for($i = $start; $i <= $end; $i += $step)
        echo date('h:i a', $i), '<br />';   
}

Output:

09:00 am
09:30 am
10:00 am
10:30 am
11:00 am
11:30 am
12:00 pm // You put am here in desired output
         // ,but I think you really wanted pm

Codepad

strtotime is another useful function for dealing with dates and times in PHP.

The PHP Manual's function reference is a great place to start when looking for how to do things yourself and taking advantage of built in functions. From that page if you do a search for 'time' you'll find the Date/Time extension which is built in to PHP. You'll see there are many functions available for dealing with date's and time's in PHP.

I would use the time to create a dateTime object. You can format your output using just the time parts, so the day portion is irrelevant. Then you can use standard functions for adding time intervals (some of them are discussed in this question). Just loop over the time addition until the end time is reached or exceeded.

This will also take care of all sorts of special cases that you'd otherwise have to handle on your own, such as AM/PM conversion and start times later than the end time (which will just wrap around to the next day).

<?php
    function intervals($start, $end, $interval)
    {       
        $start_date   = strtotime($start.':00:00');
        $end_date     = strtotime($end.'00:00');
        $current_date = $start_date;

        while($current_date <= $end_date)
        {
            echo $current_date;
            $current_date = strtotime('+ '.intval($interval).' minute', $current_date);
        }
    }
?>

I guess something like this, is what you looking for.. (untested)

this is my idea

 function myfunction($start, $end, $min_increm) {
  //just get a datetime do not matter the date
  $date = new DateTime('2000-01-01');

  //move to start hour, add 9 hour
  $start_date = $date->add(new DateInterval(PT{$start}H));
  $end date_date = $date->add(new DateInterval(PT{$end}H));
  while($date <= $end_date)
      //increment minutes and print
      echo($date->add(new DateInterval(PT{$min_increm}M))->format("H:m"));

}