我有这样的代码可以在数组中找到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";发布于 2014-05-07 17:55:09
你的剧本给了我:
Result: Nothing found但是有三个连续的日期
我改变了剧本:
<?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这是你所期望的吗?
https://stackoverflow.com/questions/23524402
复制相似问题