函数没有结束

Function doesn't come to the end

$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);

它正在加载、加载和加载,因为 isset($foo[$time]) === false 似乎总是如此。我只是找不到错误。

(可以提交更多的文字。可以提交更多的文字。可以提交更多的文字。)

strtotime('+1day') 将使用 "now" ,例如3:35pm 作为基准时间,所以你做的相当于

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

由于该值极不可能作为键出现在您的数组中,因此您的循环永远不会结束。您需要进行 >= 比较或其他操作,以检查您当前正在考虑的日期何时变得大于数组中的任何键。

在脚本开始时,您将时间设置为当前时间。然后在你的 do while 循环中,每次循环时你都会将时间增加一天。 isset($foo[time]) === false 只会 return false 并在你的时间刚好在 Mon, 23 Nov 2015 23:00:00 +0000Thu, 26 Nov 2015 23:00:00 +0000.

之前的某个时间晚上 11 点开始时退出循环

一个例子是写这个 post 时的时间戳是 1448401349 如果我加上一天我得到 1448487749。

您可能需要考虑将时间戳四舍五入到午夜,以确保在循环中发生允许其退出的冲突。 unix timestamp round to midnight