3

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
Muzz
  • 667
  • 1
  • 5
  • 11

4 Answers4

2

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.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • Thanks Paul. Yes, you're right, it was a typo on my part. I'll give this a try. – Muzz Mar 29 '12 at 20:52
  • Paul, thank you. No matter how I tried this, it wouldn't give me the right results. I was on the right track though because I was also using mktime and a for loop. I'll study your code and hopefully I'll learn something. :) Thanks again – Muzz Mar 29 '12 at 20:56
  • @Muzz I took a suggestion from another comment and removed mktime. A simple multiplication to convert hours into seconds does the trick. – Paul Mar 29 '12 at 20:57
  • Paul, can I ask you something I don't understand about your snippet? What does this do? `*=` – Muzz Mar 29 '12 at 20:59
  • `$x *= $y;` is shorthand for `$x = $x * $y`. So in the above it's short for `$step = $step * 60;` (Multiply and assign). In effect it just multiplies the variable on the left by the value on the right. – Paul Mar 29 '12 at 21:10
  • @Muzz Forgot to `@Muzz` notify you when I replied – Paul Mar 29 '12 at 21:11
0

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).

Community
  • 1
  • 1
octern
  • 4,825
  • 21
  • 38
0
<?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)

pascalvgemert
  • 1,247
  • 1
  • 13
  • 28
0

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"));

}

ab_dev86
  • 1,952
  • 16
  • 21