php在数组中查找3个连续日期

php在数组中查找3个连续日期,php,Php,我有这段代码可以在一个数组中查找3个连续的日期,但我想知道是否有更好的方法可以做到这一点 php代码: $datesarray = array('2012-10-01', '2012-10-03', '2012-10-04', '2012-10-05', '2012-10-08', '2012-10-09'); $count = 0; $result = "Nothing found"; foreach ($datesarray as $value) {

我有这段代码可以在一个数组中查找3个连续的日期,但我想知道是否有更好的方法可以做到这一点

php代码:

$datesarray = array('2012-10-01', '2012-10-03', '2012-10-04', '2012-10-05', '2012-10-08', '2012-10-09');

    $count = 0;
    $result = "Nothing found";

        foreach ($datesarray as $value) {
            if(strtotime($datesarray[$count]) - strtotime($datesarray[$count-1]) - strtotime($datesarray[$count-2]) == -1349150400) {
                $result = "3 Consecutive dates found";
            }
        $count++; 
        }  

    echo "Result: $result";
你的剧本给了我:

Result: Nothing found
但是有3个连续的日期

我更改了脚本:

<?php

$datesarray = array('2012-10-01', '2012-10-03', '2012-10-04', '2012-10-05', '2012-10-08', '2012-10-09');

$result = "Nothing found";


$diff = strtotime('2014-05-07') - strtotime('2014-05-06');

for ($i=2, $c=count($datesarray); $i<$c; ++$i) {
    if (strtotime($datesarray[$i-1]) - strtotime($datesarray[$i-2]) == $diff &&
        strtotime($datesarray[$i]) - strtotime($datesarray[$i-2]) == $diff * 2) {
            $result = "3 Consecutive dates found";                
            break;
    }

}
echo "Result: $result";  

这是您所期望的吗?

我的结果是:找到了3个连续的日期。。。不知道为什么我的代码没有找到任何东西……我在脚本中也有警告,因为在foreach中,您也检查了不存在的元素。在我的电脑上,在你的脚本中,我的值为1349042400172800,-1348956000,-1349128800,-1349042400,-1349301600。应该在代码审查中。
Result: 3 Consecutive dates found