在php中查找日期数组中的不连续性
我有一个排序的日期数组,看起来是这样的:在php中查找日期数组中的不连续性,php,Php,我有一个排序的日期数组,看起来是这样的: $dates = Array('2014-10-01','2014-10-01','2014-10-02','2014-10-03','2014-10-05'); 在这个数组中,我们有两次(有时更多,但在我的脚本中这是正常的)日期10月1日,但缺少4日 如何检测数组的第一个值和最后一个值之间是否存在不连续性(同样,我的数组是按升序日期排序的) $continued=false然后出现不连续性。可能值得研究DateTime类及其优点。特别是DateTim
$dates = Array('2014-10-01','2014-10-01','2014-10-02','2014-10-03','2014-10-05');
在这个数组中,我们有两次(有时更多,但在我的脚本中这是正常的)日期10月1日,但缺少4日
如何检测数组的第一个值和最后一个值之间是否存在不连续性(同样,我的数组是按升序日期排序的)
$continued=false
然后出现不连续性。可能值得研究DateTime
类及其优点。特别是DateTime::diff
,它返回一个DateInterval
实例。这使您能够安全地减去2个日期,并确定这些日期之间的实际时差。cf.function hasDiscontinuities($dates)
{
// remove duplicates
$dates = array_unique($dates);
// get the last day
$parts = explode('-', end($dates));
$lastDay = (int) $parts[2];
// if the amount of dates is smaller than the last day, there must be discontinuities
if (count($dates) < $lastDay) {
return true;
}
return false;
}
简而言之,我编写了一个小函数,可用于确定日期字符串数组中的奇数:
$dates = array('2014-10-01','2014-10-01','2014-10-02','2014-10-03','2014-10-05');
$obj = array();
foreach ($dates as $date)
{//create an array of DateTime instances from the $dates values
$obj[] = new DateTime($date);
}
/**
* @param array $dates (an array of DateTime instances)
* @param string $intervalProperty = 'd' (valid valus are y, m, d, h, i, s)
* @param int $interval = null (the value $intervalProperty should have)
* @return array of null|DateInterval
*/
function getOddDiffs(array $dates, $intervalProperty = 'd', $interval = null)
{
$diffs = array();
for($i=0, $j=count($dates)-1;$i<$j;++$i)
{//iterate $dates
$diff = $dates[$i]->diff($dates[$i+1]);//compute diff
if ($interval === null && $diff->{$intervalProperty})
$interval = $diff->{$intervalProperty};//set $interval if needed/possible
if ($diff->{$intervalProperty} !== $interval)
{//if interval value !== $interval (type+value check required in case $interval is null)
$diffs[] = $diff;//return the diff
}
else
{
$diffs[] = null;//null means ok
}
}
return $diffs;//return results
}
$dates=数组('2014-10-01'、'2014-10-01'、'2014-10-02'、'2014-10-03'、'2014-10-05');
$obj=array();
foreach($日期作为$日期)
{//从$dates值创建一个DateTime实例数组
$obj[]=新日期时间($date);
}
/**
*@param array$dates(日期时间实例数组)
*@param string$intervalProperty='d'(有效值为y、m、d、h、i、s)
*@param int$interval=null(值$intervalProperty应为空)
*@return数组为null | DateInterval
*/
函数getOddDiffs(数组$dates,$intervalProperty='d',$interval=null)
{
$diff=array();
对于($i=0,$j=count($dates)-1;$idiff($dates[$i+1]);//计算差异
如果($interval==null&&$diff->{$intervalProperty})
$interval=$diff->{$intervalProperty};//如果需要/可能,设置$interval
如果($diff->{$intervalProperty}!==$interval)
{//if interval value!==$interval(如果$interval为空,则需要进行类型+值检查)
$diff[]=$diff;//返回差异
}
其他的
{
$diffs[]=null;//null表示ok
}
}
return$diff;//返回结果
}
如果您希望看到它的运行:
你试过什么了吗?还有:你知道时间间隔是多少吗?我的意思是:在这种情况下,时间间隔应该是1天,总是这样吗?你从哪里得到这些日期,也许你可以确保你只得到有效的数据,而不必签入来自用户文本输入的PHP代码数据。我得到了数组使用
preg\u match\u all()的日期
在用户输入上。但我想确保用户或我的正则表达式没有错过任何日期,因为输入不应中断。干杯。在这种情况下,让用户输入一个日期,然后选择他希望输入的后续日期数不是更容易吗?然后您可以执行$start=new DateTime($userInputDate);
然后循环while($inputsequentdays--){
,在每次迭代中创建一个新的日期,将其添加到数组中。重复项也应被视为不连续项。只需确保返回值允许调用方确定发现的任何不匹配是否为0的间隔,并让他们处理它,但不确定这是否适用于覆盖数月的数组(例如,在本例中结束于11月2日)或不开始于该月1日。(抱歉,说明中没有这些详细信息)@Fredovsky:这对我建议的解决方案没有问题,这就是为什么我从一开始就建议使用DateTime
。它可以处理任何有效的日期字符串。谢谢你,这很好地完成了工作。谢谢你的时间和回答。我选择了@sgt中的一个,因为它在我看来更简单,并且只做我需要的事情,即使你的解决方案很简单可能更完整、更全面。@Fredovsky:不客气。但是:不鼓励对stackoverflow发表感谢评论。我们要求您不要发表这些评论,而是对有帮助的答案进行投票。您只能接受一个答案,但您可以投票选择任意多的答案(PS:)
function hasDiscontinuities($dates)
{
// duplicates count as discontinuities
if ($dates != array_unique($dates)) {
return true;
}
// get the last day
$parts = explode('-', end($dates));
$lastDay = (int) $parts[2];
// if the amount of dates is smaller than the last day, there must be discontinuities
if (count($dates) < $lastDay) {
return true;
}
return false;
}
function hasDiscontinuities($dates)
{
// duplicates count as discontinuities
if ($dates != array_unique($dates)) {
return true;
}
$firstDate = new DateTime($dates[0]);
$lastDate = new DateTime(end($dates));
// if the difference in days is not the same as the amount of elements in the array, there are discontinuities
if (count($dates) != $lastDate->diff($firstDate)->days + 1) {
return true;
}
return false;
}
$dates = array('2014-10-01','2014-10-01','2014-10-02','2014-10-03','2014-10-05');
$obj = array();
foreach ($dates as $date)
{//create an array of DateTime instances from the $dates values
$obj[] = new DateTime($date);
}
/**
* @param array $dates (an array of DateTime instances)
* @param string $intervalProperty = 'd' (valid valus are y, m, d, h, i, s)
* @param int $interval = null (the value $intervalProperty should have)
* @return array of null|DateInterval
*/
function getOddDiffs(array $dates, $intervalProperty = 'd', $interval = null)
{
$diffs = array();
for($i=0, $j=count($dates)-1;$i<$j;++$i)
{//iterate $dates
$diff = $dates[$i]->diff($dates[$i+1]);//compute diff
if ($interval === null && $diff->{$intervalProperty})
$interval = $diff->{$intervalProperty};//set $interval if needed/possible
if ($diff->{$intervalProperty} !== $interval)
{//if interval value !== $interval (type+value check required in case $interval is null)
$diffs[] = $diff;//return the diff
}
else
{
$diffs[] = null;//null means ok
}
}
return $diffs;//return results
}