İki ardışık tarih arasındaki gün büyük sayısını hesaplayın

3 Cevap php

ISO tarihleri ​​bir dizi varsa, nasıl diziden iki sıralı tarih arasındaki en gün hesaplamak istiyorsunuz?

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

Ben bir döngü, yineleme değişkeni çeşit ve bir tür gerekir düşünüyorum. Ben oldukça bu anlamaya olamaz.

Bu aslında MYSQL çıktı ediliyor.

3 Cevap

Burada PHP bunu nasıl olduğunu:

<?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);

?>

(O öğe vardır bir az zaman yürütür) öğeleri atmak döngüler ve her biri karşılaştırır. Karşılaştırma bir sonraki daha büyük ise, bu max güncelleştirir. Zaman saniye, yani biz gün içine saniye dönüştürmek üzerinde döngü peşinde.

Bu PHP script size büyük aralığını verecek

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

$ Maxinterval = 0;

if (count ($ dizi)> 1) {

for($i = 0; $i < count($array) - 1; $i++){

$days = (strtotime( $array]$i] ) - strtotime( $array[$i + 1] )) / 86400 + 1;

if($days > $maxinterval) $maxinterval = $days; 

}

} ?>

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

  • A) dizi artan düzende tarihlerin bir listesini içerir. Görev dizideki consecutive tarihleri ​​arasında longuest dönemi (gün sayısı olarak ifade edilmiştir) bulmaktır.
  • B) dizi mutlaka sıralanır. Görev dizideki any two dates arasındaki longuest dönemi (gün sayısı olarak expr.) bulmak için

Aşağıdaki sorunun "B" anlayışına bir cevap sağlar. "A" bir yanıt için, dcneiner çözümünü görmek


Hayır Sıralama gerekli! ...

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);

?>