-3

I've tried building and placing the while loop in different ways and different locations and looking at $val as the right/wrong variable to be placing in the while loop but I'm just not getting it. I expected output to be: 815 830 845 900....... until reaching 1900.

Instead I got 815 then 1915 1930 1945 up through 0000 then it cycles over starting at 1915. Someone please tell me what i have placed in the wrong location or what variable I've used wrongly.

date_default_timezone_set('America/New_York');
$st='800';
$et='1900';
$frac = 900;
$x = $et;
$val=0;

if (strlen($st) < 4) {$st = "0".$st;}
$current_time = strtotime(date($st));
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$val = date('Gi', $new_time);
echo $val."<br>";

while ($val !== $et){
if (strlen($val) < 4) {$st = "0".$val;}
$current_time = strtotime('+ 15 minutes',date($val));
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$val = date('Gi', $new_time);
echo $val."<br>";
}

NOTE TO THE - MARKDOWN PLAYERS -- Edited POST ANSWER: You can bash the question all you want, but the fact is it was a clear question, clear objective trying to be reached and it was a clear attempt (one of several) to code it properly that failed, so bad code, not bad effort. So eventually I asked for help. Your using the markdown system the wrong way and for the wrong reason, but whatever I have little expectation that you care. Thanks to those that made the effort actually be constructive and teach/help.

DMSJax
  • 1,709
  • 4
  • 22
  • 35
  • you want the time, in 15 minute intervilles from when till 1900 ? –  Feb 26 '15 at 20:09
  • I'd avoid all the date/time math and just do a loop on integers. When you get your result, then convert to date/time. That or find the number of seconds between the start and end time, and do the math on that. – Kevin Seifert Feb 26 '15 at 20:13
  • @dagon yes, I'm trying to update the value of $val on each loop by 15 minute increments. starting value is $st and ending value is $et, both of which may change but in this case i just set the values – DMSJax Feb 26 '15 at 20:15
  • @Dagon I thought '$current_time = strtotime('+ 15 minutes',date($val));' in the while loop would do that, but not so. 800 is a string value passed in/on from a $_POST in an earlier part of the scripts – DMSJax Feb 26 '15 at 20:18

2 Answers2

0

With unelegant for loop:

for ($i = 800, $t= 1900; $i <= $t; $i += 15) { 
    printf('%1$3d <br>', $i); 
    if ($i%100 >= 45) $i += 40; 
}

With DatePeriod:

$timezone = new DateTimeZone("America/New_York");
$begin = new DateTime("now", $timezone);
$begin->setTime(8, 0, 0);
$end = new DateTime("now", $timezone);
$end->setTime(19, 0, 0);

$interval = new DateInterval('PT15M');
$end->add($interval);

$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("Gi") . "<br>";
}

With generators:

function xmins($start, $end, $step = 15) {
    if ($start < $end) {
        if (($step <= 0) || ($step >= 40)) {
            throw new LogicException('Step must be in range 1-40');
        }

        for ($i = $start; $i <= $end; $i += $step) {
            if (60 <= $temp = $i % 100) {
                $i += 40;
            }
            yield $i;
            if ((60-$step) === $temp = $i % 100) {
                $i += 100 - $temp - $step;
            }
        }
    }
}

foreach (xmins(800,1900,15) as $i) {
    printf('%1$3d<br>', $i);
}
OIS
  • 9,833
  • 3
  • 32
  • 41
  • Thank you, in order to accept the $st and $et strings I had to modify it slight to include the following, but either way thank you. My approach was i guess way off the mark. `if (strlen($st) < 4) {$st = "0".$st;} if (strlen($et) < 4) {$st = "0".$st;} $begin->setTime(substr($st,0,2),substr($st, -2),0); $end->setTime(substr($et,0,2),substr($et, -2),0);` – DMSJax Feb 26 '15 at 20:36
  • @DMSJax The DateTime classes are really helpfull dealing with this stuff, and I'm sure will come in handy several times. :) – OIS Feb 26 '15 at 20:49
0

Using DateInterval:

$date = new DateTime('2015-01-01 08:00:00');
$mins = new DateInterval('PT15M');
do {
    $date->add($mins);
    echo $date->format('Hi')."\n";
} while($date->format('Hi') < 1900);