devzone
devzone

Reputation: 305

Loop through range of time in half hour increments

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.

Upvotes: 2

Views: 12371

Answers (9)

mickmackusa
mickmackusa

Reputation: 47903

You can loop through a DateTime period after establishing the first start and last start time (the end time is "inclusive" thanks to the fourth parameter of the dateperiod declaration). Set the interval for 30 minutes, then loop. As you iterate, modify() the current datetime object by an additional 30 minutes to create the end time for each range.

Notice that I separated the a (am/pm) expression -- this is because you wish for 11:30 - 12:00 to say am. If modify() was called first, then a would be pm. Consequently, I've changed the order of the placeholders in the first parameter of printf() -- %3$s is written before %2$s.

This is also a perfect time to inform you that there is no benefit in bloating your html markup with value attribute values that are identical to your option text. Form submissions and javascript operations will still work exactly the same without error. When they are the same, do not repeat the option text as the option value.

Code: (Demo)

$period = new DatePeriod(
    new DateTime('today 06:00:00'),
    new DateInterval('PT30M'),
    new DateTime('today 22:00:00'),
    DatePeriod::INCLUDE_END_DATE
);

foreach ($period as $dt) {
    printf(
        '<option>%1$s - %3$s %2$s</option>' . "\n",
        $dt->format('h:i'),
        $dt->format('a'),
        $dt->modify('+30 minutes')->format('h:i')
    );
}

Output:

<option>06:00 - 06:30 am</option>
<option>06:30 - 07:00 am</option>
<option>07:00 - 07:30 am</option>
<option>07:30 - 08:00 am</option>
<option>08:00 - 08:30 am</option>
<option>08:30 - 09:00 am</option>
<option>09:00 - 09:30 am</option>
<option>09:30 - 10:00 am</option>
<option>10:00 - 10:30 am</option>
<option>10:30 - 11:00 am</option>
<option>11:00 - 11:30 am</option>
<option>11:30 - 12:00 am</option>
<option>12:00 - 12:30 pm</option>
<option>12:30 - 01:00 pm</option>
<option>01:00 - 01:30 pm</option>
<option>01:30 - 02:00 pm</option>
<option>02:00 - 02:30 pm</option>
<option>02:30 - 03:00 pm</option>
<option>03:00 - 03:30 pm</option>
<option>03:30 - 04:00 pm</option>
<option>04:00 - 04:30 pm</option>
<option>04:30 - 05:00 pm</option>
<option>05:00 - 05:30 pm</option>
<option>05:30 - 06:00 pm</option>
<option>06:00 - 06:30 pm</option>
<option>06:30 - 07:00 pm</option>
<option>07:00 - 07:30 pm</option>
<option>07:30 - 08:00 pm</option>
<option>08:00 - 08:30 pm</option>
<option>08:30 - 09:00 pm</option>
<option>09:00 - 09:30 pm</option>
<option>09:30 - 10:00 pm</option>
<option>10:00 - 10:30 pm</option>

Upvotes: 1

Vinay
Vinay

Reputation: 2594

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

Upvotes: 3

Wolfy
Wolfy

Reputation: 4373

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>

Upvotes: 1

Vagif
Vagif

Reputation: 364

<?php

    $timeFormat = "%02d:%02d";
    $startHour = 6;
    $endHour = 12;
    $timeInterval = 30; // in minutes

    $currentHour = $startHour;
    $currentMinute = 0;
    while ($currentHour <= $endHour) {
        $timeRange = sprintf($timeFormat, $currentHour, $currentMinute) . ($currentHour < 12 ? " am" : " pm");

        $currentMinute = $currentMinute + $timeInterval;
        if ($currentMinute > 59) {
            $currentHour++;
            $currentMinute = $currentMinute - 60;
        }

        $timeRange .= " - " . sprintf($timeFormat, $currentHour, $currentMinute) . ($currentHour < 12 ? " am" : " pm");

        if ($currentHour <= $endHour) {
            echo "## " . $timeRange . "\n";         
        }       
    }

?>

Upvotes: 0

tradyblix
tradyblix

Reputation: 7569

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

Upvotes: 2

cwallenpoole
cwallenpoole

Reputation: 82028

This simply loops through every half our and then calls date on each.

$inc   = 30 * 60;
$start = (strtotime('6AM')); // 6  AM
$end   = (strtotime('10PM')); // 10 PM

echo "<select>";
for( $i = $start; $i <= $end; $i += $inc )
{
    // to the standart format
    $range = date( 'g:i', $i ) . ' - ' . 
             // $inc = 30 minutes so this will be .5 hr later
             date( 'g:i A', $i + $inc );
    echo "<option value=\"$range\">$range</option>" . PHP_EOL;
}
echo "</select>";

Upvotes: 0

deceze
deceze

Reputation: 522175

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

Upvotes: 7

JohnP
JohnP

Reputation: 50019

Something like this should do it. You're basically adding blocks of 30 mins to a time and looping till you get the correct number of options.

//get the timestamp for 6.30 am
$time   = strtotime('6.30 am'); //not important what the date is.

//start adding 30mins
$times      = array();
$halfHour   = 60 * 30;
$blocks     = 12; //adjust this to get the number of options
while ($blocks) {
    $times[]  = date('h:i', $time) . ' - ' . date('h:i a', ($time += $halfHour)); //I keep incrementing the time by half an hour
    $blocks--;
}
var_dump($times);

Upvotes: 0

Abhay
Abhay

Reputation: 6645

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

Upvotes: 1

Related Questions