1
$time = time();

$foo = array(
    1448319600 => array(
        array(
            'foo' => 'bar'
        ),
        array(
            'bar' => 'foo'
        )
    ),
    1448578800 => array(
        array(
            'foo2' => 'bar2'
        ),
        array(
            'bar2' => 'foo2'
        )
    )
);

function bar($time, $foo) {
    $count = 0;
    do {
        $count++;
        $time = strtotime('+1 day', $time);
    } while (isset($foo[$time]) === false);
    return array(
        'count' => $count,
        'foo' => $foo[$time]
    );
}

bar($time, $foo);

It's loading and loading and loading, because isset($foo[$time]) === false always seems to be true. I just can't find the error.

(Some more text to be able to submit. Some more text to be able to submit. Some more text to be able to submit.)

Ben
  • 1,550
  • 1
  • 16
  • 24
  • In my opinion it is of course depending on the `start value` you specify by executing `bar` for the first time. Becaue you always add `1 day` which resolves to `86400 seconds` and the timestamps you specified are `11/26/2015` and `11/25/2015` both at `11:00 pm`. So if you call `bar` with an initial `$time` of something different to `11:00 pm` it won't work of course. – Markus Safar Nov 24 '15 at 21:38
  • Actually, `1448319600` should represent `11/24/2015 00:00` and `1448578800` should represent `11/27/2015 00:00`. – Ben Nov 24 '15 at 21:44

2 Answers2

3

strtotime('+1day') is going to use "now" , e.g. 3:35pm as the base time, so you're doing the equivalent of

$now = time(); // 1448400979
$tomorrow = strtotime('+1 day', $now); // 1448487379
while(!isset($arr[$tomorrow])) { ... }

Since that value is highly unlikely to ever be in your array as a key, your loop never ends. You need to do a >= comparison or something, to check when your currently-being-considered date becomes larger than any key in your array.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • But he specifies the initial as parameter to `strtotime`, right? – Markus Safar Nov 24 '15 at 21:40
  • 1
    yeah, but it's `time()`, so effectively always "now", then adds a fixed 86,400 (1 days' worth of seconds) each time. there's only TWO time points in the array, and the odds of hitting either of them with a semi-randomish time() value are unlikely. it's like kinda of like looping `$i = 0,2,4,6,etc..`. and wondering why it doesn't stop because you're testing for `$i=5`, which never occurs – Marc B Nov 24 '15 at 21:42
  • hahaha - exactly, I didn't catch the first line of the code :D I already suspected (and commented) that as a possible reason :-) Gave you an upvote ;-) – Markus Safar Nov 24 '15 at 21:44
  • Thank you! With `$time = strtotime('today')` it works. :) – Ben Nov 24 '15 at 21:47
1

At the start of your script you are setting time to the current time. Then in your do while loop you are incrementing the time by one day each time it loops. isset($foo[time]) === false will only return false and exit the loop when your time started at exactly 11pm sometime before Mon, 23 Nov 2015 23:00:00 +0000 or Thu, 26 Nov 2015 23:00:00 +0000.

An example being the timestamp while writing this post is 1448401349 If I add one day to it I get 1448487749.

You might want to look into rounding your timestamps to midnight to make sure you get collisions in your loop that allow it to exit. unix timestamp round to midnight

Community
  • 1
  • 1
Joseph Evans
  • 1,360
  • 9
  • 14