function eme_get_recurrence_days($recurrence)
{
    global $eme_timezone;
    $matching_days = array();
    $eme_date_obj = new ExpressiveDate(null, $eme_timezone);
    if ($recurrence['recurrence_freq'] == 'specific') {
        $specific_days = explode(",", $recurrence['recurrence_specific_days']);
        foreach ($specific_days as $day) {
            $eme_date_obj->setTimestampFromString($day);
            array_push($matching_days, $eme_date_obj->format('U'));
        }
        return $matching_days;
    }
    $start_date_obj = new ExpressiveDate($recurrence['recurrence_start_date'], $eme_timezone);
    $end_date_obj = new ExpressiveDate($recurrence['recurrence_end_date'], $eme_timezone);
    $last_week_start = array(25, 22, 25, 24, 25, 24, 25, 25, 24, 25, 24, 25);
    $weekdays = explode(",", $recurrence['recurrence_byday']);
    $counter = 0;
    $daycounter = 0;
    $weekcounter = 0;
    $monthcounter = 0;
    $start_monthday = $start_date_obj->format('j');
    $cycle_date_obj = $start_date_obj->copy();
    while ($cycle_date_obj->lessOrEqualTo($end_date_obj)) {
        $cycle_date = $cycle_date_obj->format('U');
        $style = "";
        $monthweek = floor(($cycle_date_obj->format('d') - 1) / 7) + 1;
        if ($recurrence['recurrence_freq'] == 'daily') {
            if ($daycounter % $recurrence['recurrence_interval'] == 0) {
                array_push($matching_days, $cycle_date);
            }
        }
        if ($recurrence['recurrence_freq'] == 'weekly') {
            if (!$recurrence['recurrence_byday'] && eme_iso_N_date_value($cycle_date_obj) == eme_iso_N_date_value($start_date_obj)) {
                // no specific days given, so we use 7 days as interval
                //if($daycounter % 7*$recurrence['recurrence_interval'] == 0 ) {
                if ($weekcounter % $recurrence['recurrence_interval'] == 0) {
                    array_push($matching_days, $cycle_date);
                }
            } elseif (in_array(eme_iso_N_date_value($cycle_date_obj), $weekdays)) {
                // specific days, so we only check for those days
                if ($weekcounter % $recurrence['recurrence_interval'] == 0) {
                    array_push($matching_days, $cycle_date);
                }
            }
        }
        if ($recurrence['recurrence_freq'] == 'monthly') {
            $monthday = $cycle_date_obj->format('j');
            $month = $cycle_date_obj->format('n');
            // if recurrence_byweekno=0 ==> means to use the startday as repeating day
            if ($recurrence['recurrence_byweekno'] == 0) {
                if ($monthday == $start_monthday) {
                    if ($monthcounter % $recurrence['recurrence_interval'] == 0) {
                        array_push($matching_days, $cycle_date);
                    }
                    $counter++;
                }
            } elseif (in_array(eme_iso_N_date_value($cycle_date_obj), $weekdays)) {
                if ($recurrence['recurrence_byweekno'] == -1 && $monthday >= $last_week_start[$month - 1]) {
                    if ($monthcounter % $recurrence['recurrence_interval'] == 0) {
                        array_push($matching_days, $cycle_date);
                    }
                } elseif ($recurrence['recurrence_byweekno'] == $monthweek) {
                    if ($monthcounter % $recurrence['recurrence_interval'] == 0) {
                        array_push($matching_days, $cycle_date);
                    }
                }
                $counter++;
            }
        }
        $cycle_date_obj->addOneDay();
        $daycounter++;
        if ($daycounter % 7 == 0) {
            $weekcounter++;
        }
        if ($cycle_date_obj->format('j') == 1) {
            $monthcounter++;
        }
    }
    return $matching_days;
}
Example #2
0
function eme_get_recurrence_days($recurrence)
{
    $matching_days = array();
    if ($recurrence['recurrence_freq'] == 'specific') {
        $specific_days = explode(",", $recurrence['recurrence_specific_days']);
        foreach ($specific_days as $day) {
            $date = mktime(0, 0, 0, substr($day, 5, 2), substr($day, 8, 2), substr($day, 0, 4));
            array_push($matching_days, $date);
        }
        return $matching_days;
    }
    $start_date = mktime(0, 0, 0, substr($recurrence['recurrence_start_date'], 5, 2), substr($recurrence['recurrence_start_date'], 8, 2), substr($recurrence['recurrence_start_date'], 0, 4));
    $end_date = mktime(0, 0, 0, substr($recurrence['recurrence_end_date'], 5, 2), substr($recurrence['recurrence_end_date'], 8, 2), substr($recurrence['recurrence_end_date'], 0, 4));
    // $every_keys = array('every' => 1, 'every_second' => 2, 'every_third' => 3, 'every_fourth' => 4);
    // $every_N = $every_keys[$recurrence['recurrence_modifier']];
    // $month_position_keys = array('first_of_month'=>1, 'second_of_month' => 2, 'third_of_month' => 3, 'fourth_of_month' => 4);
    // $month_position = $month_position_keys[$recurrence['recurrence_modifier']];
    $last_week_start = array(25, 22, 25, 24, 25, 24, 25, 25, 24, 25, 24, 25);
    $weekdays = explode(",", $recurrence['recurrence_byday']);
    $counter = 0;
    $daycounter = 0;
    $weekcounter = 0;
    $monthcounter = 0;
    $start_monthday = date("j", $start_date);
    $cycle_date = $start_date;
    while ($cycle_date <= $end_date) {
        //echo (date("d-M-Y", $cycle_date));
        $style = "";
        $monthweek = floor((date("d", $cycle_date) - 1) / 7) + 1;
        if ($recurrence['recurrence_freq'] == 'daily') {
            if ($daycounter % $recurrence['recurrence_interval'] == 0) {
                array_push($matching_days, $cycle_date);
            }
        }
        if ($recurrence['recurrence_freq'] == 'weekly') {
            if (!$recurrence['recurrence_byday'] && eme_iso_N_date_value($cycle_date) == eme_iso_N_date_value($start_date)) {
                // no specific days given, so we use 7 days as interval
                //if($daycounter % 7*$recurrence['recurrence_interval'] == 0 ) {
                if ($weekcounter % $recurrence['recurrence_interval'] == 0) {
                    array_push($matching_days, $cycle_date);
                }
            } elseif (in_array(eme_iso_N_date_value($cycle_date), $weekdays)) {
                // specific days, so we only check for those days
                if ($weekcounter % $recurrence['recurrence_interval'] == 0) {
                    array_push($matching_days, $cycle_date);
                }
            }
        }
        if ($recurrence['recurrence_freq'] == 'monthly') {
            $monthday = date("j", $cycle_date);
            $month = date("n", $cycle_date);
            // if recurrence_byweekno=0 ==> means to use the startday as repeating day
            if ($recurrence['recurrence_byweekno'] == 0) {
                if ($monthday == $start_monthday) {
                    if ($monthcounter % $recurrence['recurrence_interval'] == 0) {
                        array_push($matching_days, $cycle_date);
                    }
                    $counter++;
                }
            } elseif (in_array(eme_iso_N_date_value($cycle_date), $weekdays)) {
                if ($recurrence['recurrence_byweekno'] == -1 && $monthday >= $last_week_start[$month - 1]) {
                    if ($monthcounter % $recurrence['recurrence_interval'] == 0) {
                        array_push($matching_days, $cycle_date);
                    }
                } elseif ($recurrence['recurrence_byweekno'] == $monthweek) {
                    if ($monthcounter % $recurrence['recurrence_interval'] == 0) {
                        array_push($matching_days, $cycle_date);
                    }
                }
                $counter++;
            }
        }
        $cycle_date = eme_unixdate_calc("+1 day", $cycle_date);
        //adding a day
        $daycounter++;
        if ($daycounter % 7 == 0) {
            $weekcounter++;
        }
        if (date("j", $cycle_date) == 1) {
            $monthcounter++;
        }
    }
    return $matching_days;
}