开发者

Calculate greatest number of days between two consecutive dates

If you have an array o开发者_开发知识库f ISO dates, how would you calculate the most days between two sequential dates from the array?

$array = array('2009-03-11', '2009-03-12', '2009-04-12', '2009-05-03', '2009-10-30');

I think I need a loop, some sort of iterating variable and a sort. I can't quite figure it out.

This is actually being output from MYSQL.


Here is how you can do it in PHP:

<?php
$array = array('2009-03-11', '2009-03-12', '2009-04-12', '2009-05-03', '2009-10-30');

# PHP was throwing errors until I set this
# it may be unnecessary depending on where you
# are using your code:
date_default_timezone_set("GMT");

$max = 0;
if( count($array) > 1 ){
    for($i = 0; $i < count($array) - 1; $i++){
        $start = strtotime( $array[$i] );
        $end   = strtotime( $array[$i + 1] );

        $diff  = $end - $start;
        if($diff > $max) $max = $diff; 
    }
}

$max = $max / (60*60*24);

?>

It loops throw your items (it executes one less time than there are number of items) and compares each one. If the comparison is larger than the next, it updates max. Time is in seconds, so after the loop is over we convert the seconds into days.


This PHP script will give you the largest interval

1 ){ for($i = 0; $i $maxinterval) $maxinterval = $days; } } ?>


EDIT:
As [originally] worded the question can be understood in [at least ;-)] two ways:

  • A) The array contains a list of dates in ascending order. The task is to find the longuest period (expressed in number of days) between to consecutive dates in the array.
  • B) The array is not necessarily sorted. The task is to find the longuest period (expr. in number of days) between any two dates in the array

The following provides an answer to the "B" understanding of the question. For a response to "A", see dcneiner's solution


No Sorting needed!...

If it comes from MySQL, you may have this DBMS returns directly the MIN and MAX values for the considered list.
EDIT: As indicated by Darkerstar, the way the way the data is structured [and also the existing SQL query which returns the complete list as indicated in the question] generally dictate the way the query which produces the MIN and MAX value should be structured.
Maybe something like this:

SELECT MIN(the_date_field), MAX(the_date_field)
FROM the_table
WHERE -- whatever where conditions if any
--Note: no GROUP BY needed

If, somehow, you cannot use SQL, a single pass through the list will allow you to obtain the MIN and MAX value in the list (in O(n) time, that is).
Algorithm is trivial:
Set Min and Max Value to first item in [unsorted] list.
Iterate through each following item in the list, comparing it with the Min Value and replacing it if found smaller, and doing like-wise for the Max value...

With Min and Max values in hand, a simple difference gives the max number of days...
In PHP, it's looks like the following:

<?php
$array = array('2009-03-11', '2009-03-12', '2009-04-12', '2009-05-03', '2009-10-30');

# may need this as suggested by dcneiner
date_default_timezone_set("GMT");

$max = $array[0];
$min = $max;
for($i = 1; $i < count($array); $i++){
    // Note that since the strings in the array are in the format YYYY-MM-DD,
    // they can be compared as-is without requiring say strtotime conversion.
    if ($array[$i] < $min)
        $min = $array[$i];
    if ($array[$i] > $max)
        $max = $array[$i];
}
$day_count = (strtotime($max) - strtotime($min)) / (60*60*24);

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜