我有这个代码在一个数组中找到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";
答案 0 :(得分:1)
你的脚本给了我:
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";
,结果是
Result: 3 Consecutive dates found
这是你所期望的吗?