[英]php find 3 consecutive dates in array
i have this code to find 3 consecutive dates in an array but i'm wondering if there is a better way to do this? 我有这段代码来查找数组中的3个连续的日期,但我想知道是否有更好的方法来执行此操作?
php code: 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";
Your script gives me: 您的脚本给了我:
Result: Nothing found
but there are 3 consecutive dates 但连续3个日期
I've changed the script: 我已经更改了脚本:
<?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";
and the result is 结果是
Result: 3 Consecutive dates found
Is this what you expected? 这是您所期望的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.