Páginas

miércoles, 8 de agosto de 2012

Script de comparación entre rangos de fechas

Este es un pequeño script que realice para dar una respuesta en foros del web, y me pareció que vale la pena compartirlo, ando un poco fiebruo con eso de los closures, por lo que me hice uno para hacer este script


date_default_timezone_set('UTC');

$rangeBettweenRange = function(array $range1, array $range2) {
    $from1 = new DateTime($range1[0]);
    $to1 = new DateTime($range1[1]);
    $from2 = new DateTime($range2[0]);
    $to2 = new DateTime($range2[1]);    

    $range1Diff = $from1->diff($to1);    

    for($i = 0; $i <= $range1Diff->days; $i++) {    
        if($from1 >= $from2 && $from1 <= $to2) {
            $return[$from1->format('d-m-Y')] =  "in the range 
";
        } else {
            $return[$from1->format('d-m-Y')] = "out the range
";
        }
        $from1->modify("+1 day");        
    }
    return $return;
};

El resultado es algo como esto
Array
(
    [01-01-2012] => out the range

    [02-01-2012] => out the range

    [03-01-2012] => out the range

    [04-01-2012] => out the range

    [05-01-2012] => out the range

    [06-01-2012] => out the range

    [07-01-2012] => out the range

    [08-01-2012] => out the range

    [09-01-2012] => out the range

    [10-01-2012] => out the range

    [11-01-2012] => out the range

    [12-01-2012] => out the range

    [13-01-2012] => out the range

    [14-01-2012] => out the range

    [15-01-2012] => in the range 

    [16-01-2012] => in the range 

    [17-01-2012] => in the range 

    [18-01-2012] => in the range 

    [19-01-2012] => in the range 

    [20-01-2012] => in the range 

    [21-01-2012] => in the range 

    [22-01-2012] => in the range 

    [23-01-2012] => in the range 

    [24-01-2012] => in the range 

    [25-01-2012] => in the range 

    [26-01-2012] => in the range 

    [27-01-2012] => in the range 

    [28-01-2012] => in the range 

    [29-01-2012] => in the range 

    [30-01-2012] => in the range 

    [31-01-2012] => in the range 

)
el que hacer con el resultado ya queda del desarrollador. Espero que el script sea de utilidad, saludos