Loop through range of time in half hour increments

11,440

Solution 1

$time = mktime(0, 0, 0, 1, 1);
for ($i = 0; $i < 86400; $i += 1800) {  // 1800 = half hour, 86400 = one day
    printf('<option value="%1$s-%2$s">%1$s-%2$s</option>',
           date('g:i', $time + $i), date('g:i a', $time + $i + 1800));
}

Fiddle with the starting time and end condition as required.

Solution 2

<select style='width:250px;' name='days' onchange='return timeSchedvalue(this.value)'>
<?php
     $time = '6:00'; // start
     for ($i = 0; $i <= 12; $i++)
     {
         $prev = date('g:i a', strtotime($time)); // format the start time
         $next = strtotime('+30mins', strtotime($time)); // add 30 mins
         $time = date('g:i a', $next); // format the next time
         echo "<option value=\"$prev - $time\">$prev - $time</option>";
     }
 ?>
</select>

Solution 3

Use this if you like.

for ($x = 6,$x1 = 1; $x < 22.5; $x+=0.5,$x1++) 
{ 
$gen_date=date('h:i A', mktime(0, 0, 0) + (60 * $x * 60));
$temp=$x+0.5;
$gen_date1=date('h:i A', mktime(0, 0, 0) + (60 * $temp * 60));
echo $x1.'='.$gen_date.'-'.$gen_date1.'<br>';
}

Use $x1 as index. edit as per your need

Solution 4

Well, the easiest way is to keep an array of all your intervals and loop through it, perhaps like:

array ('06:00', '06:30', '07:00', '07:30'... );

Or may be like:

array ('06:00 - 06:30', '06:30 - 07:00', '07:00 - 07:30'...);

Solution 5

You can create time with mktime and format it with date.

In your case you need to create a start time 6:00 am -> mktime(6, 0, 0, 0, 0, 0)

then you need to add 30 min for each next time...

This can be easily done in a for loop:

<select style='width:250px;' name='days' onchange='return timeSchedvalue(this.value)'>
<?php
 for ($i = 0; $i <= 960; $i+=30) {
    $time1 = date('h:i a', mktime(6, $i, 0, 0, 0, 0));
    $time2 = date('h:i a', mktime(6, $i+30, 0, 0, 0, 0));
    echo "<option value='" . $time1 . " - " . $time2 . "'>" .$time1 . " - " . $time2 . "</option>";
}
?>
</select>
Share:
11,440

Related videos on Youtube

devzone
Author by

devzone

Updated on June 04, 2022

Comments

  • devzone
    devzone almost 2 years

    I want to know how to loop a time using 30-minute steps in php. I want the output to be like this:

    <select style='width:250px;' name='days' onchange='return timeSchedvalue(this.value)'>
      <option value='6:00-6:30 am'>6:00-7:30 am</option>
      <option value='6:30-7:00 am'>6:30-7:00 am</option>
      <option value='7:30-8:00 am'>7:30-8:00 am</option>
      <option value='8:00-8:30 am'>8:00-8:30 am</option>
      <option value='8:30-9:00 am'>8:30-9:00 am</option>
      <option value='9:00-9:30 am'>9:00-9:30 am</option>
      <option value='9:30-10:00 am'>9:30-10:00 am</option>
      <option value='10:00-10:30 am'>10:00-10:30 am</option>
      <option value='10:30-11:00 am'>10:30-11:00 am</option>
      <option value='11:00-11:30 am'>11:00-11:30 am</option>
      <option value='11:30-12:00 am'>11:30-12:00 am</option>
      <option value='12:00-12:30 pm'>12:00-12:30 pm</option>
    </select>
    

    The loop will starts at 6:00 am - 6:30 am and it will end by 10:00 pm - 10:30 pm.

  • Abhay
    Abhay almost 13 years
    Why a down vote on my answer? My reputation page says -2 for the down vote? I'll appreciate if the voter may please explain his/her thought
  • foxybagga
    foxybagga over 11 years
    I did not down vote but I believe it was because you hard coded the values.
  • Abhay
    Abhay over 11 years
    Yeah, may be! I only wanted to give an idea to the solution ;-). Anyways, it doesn't make sense to make it dynamic unless the intervals might change.
  • zoltar
    zoltar about 11 years
    the purpose of dynamic coding is to write more scalable, simplified code which in turn becomes even more elegant. what you wrote was technically feasible but not the best answer, perhaps. i would use it, though, in cases of extremely close deadlines, lol