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
https://forums.phpfreaks.com/topic/87741-solved-looping-from-one-time-to-another/
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>";
}
?>

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

}

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.