Programación

PHP

Funciones para obtener fechas de semana santa por año

Última actualización: 28-10-2017 18:22
Clase cl_holyWeek con funciones para obtener: Importante:

Código


<?php
    // :: Funciones comunes para otras clases ::
    class cl_common
    {
        // Devuelve HTML de legibilidad facilitada de un arreglo
        function showArray($ar)
        {
            return '<pre>' print_r($artrue) . '</pre>';
        }
    }

    // :: Cálculo de fechas de semana santa a partir de la función easter_date de PHP ::
    class cl_holyWeek extends cl_common
    {
        // Devuelve la fecha del "Domingo de Resurrección" del año solicitado
        function getLastDateOfYear($year 0)
        {
            if ($year == 0$year date('Y');
            return date('Y-m-d'easter_date($year));
        }

        // Devuelve arreglo con fechas de la semana santa del año solicitado
        function getDatesOfYear($year 0)
        {
            if ($year == 0$year date('Y');
            $r NULL;
            $diff1Day = new DateInterval('P1D');
            $diff1Day->invert 1;
            $dt = new DateTime((date 'Y-m-d'easter_date($year))));
            $r =  array($dt->format('Y-m-d'));
            for ($i 1$i <= 3$i++)
            {
                $dt->add($diff1Day);
                $r[] = $dt->format('Y-m-d');
            }
            return array_reverse($r);
        }

        // Devuelve arreglo con fechas de la semana santa del rango de años solicitado
        function getDatesOfYears($starterYear$endingYear)
        {
            $r $this->getDatesOfYear($starterYear);
            for ($i $starterYear 1$i <= $endingYear$i++)
                $r array_merge($r$this->getDatesOfYear($i));
            return $r;
        }

    }

    $o = new cl_holyWeek();
    echo $o->getLastDateOfYear() . '<hr/>';
    echo $o->showArray($o->getDatesOfYear()) . '<hr/>';
    echo $o->showArray($o->getDatesOfYears(20162017)) . '<hr/>';
?>
    

Resultado

2017-04-16

Array
(
    [0] => 2017-04-13
    [1] => 2017-04-14
    [2] => 2017-04-15
    [3] => 2017-04-16
)

Array
(
    [0] => 2016-03-24
    [1] => 2016-03-25
    [2] => 2016-03-26
    [3] => 2016-03-27
    [4] => 2017-04-13
    [5] => 2017-04-14
    [6] => 2017-04-15
    [7] => 2017-04-16
)