Jump to content

[SOLVED] Looping from one time to another?


Solarpitch

Recommended Posts

Hi Guys,

 

just wondering whats the best way of even how you would do this. I have two select boxes where a user chooses a start time and a finish time. If the user selects...

 

START:    6:00 am

FINISH:  10:00 am

 

... I want to display a list of 15 min intervals between the two times

 

eg:

 

6:00 am, 6:15 am, 6:30 am, 6:45 am, 7:00 am, 7:15 am... right up until 10:00 am

 

I take it you would use a loop but I am not sure how.

 

Thanks

Link to comment
Share on other sites

<?PHP
$start="6:00";
$finish="10:00";
$a=explode(":",$start);
$start_hr=$a[0];
$a=explode(":",$finish);
$finish_hr=$a[0];

for($i=$start_hr;$i<=$finish_hr;$i++){
echo $i.":00"."<BR>" ;
echo $i.":15"."<BR>";
echo $i.":30"."<BR>";
echo $i.":45"."<BR>";
}
?>

Link to comment
Share on other sites

<?PHP

$start="6:00";

$finish="10:00";

$a=explode(":",$start);

$start_hr=$a[0];

$a=explode(":",$finish);

$finish_hr=$a[0];

$int_interval=15;

$interval=00;

 

for($i=$start_hr;$i<=$finish_hr;$i++) {

      if ($interval > 45) $interval=00;

echo $i.":".$interval."<BR>" ;

      $interval += $int_interval;

}

?>

Link to comment
Share on other sites

I realize you marked this "Solved", but here's one more solution that uses the strtotime() and date() functions:

<?php
$st = strtotime(date('Y-m-d') . '6:00 am');
$en = strtotime(date('Y-m-d') . '10:00 am');
$int = 15 * 60; // number of seconds in 15 minutes
for ($i = $st; $i <= $en; $i += $int)
        echo date('G:i a',$i) . "<br>\n";
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.