コード例 #1
0
/**
 * Fetches the next occurance for a repeating event.
 *
 * @param array The event array
 * @param array The range of start/end timestamps
 * @param int The last occurance of this event
 * @param boolean True if this is our first iteration of this function (Does some special optimised calculations on false)
 * @return int The next occurance timestamp
 */
function fetch_next_occurance($event, $range, $last_occurance, $first = false)
{
    $new_time = $last_occurance;
    $repeats = $event['repeats'];
    $start_day = explode("-", gmdate("j-n-Y", $event['starttime_user']));
    $start_date = gmmktime(0, 0, 0, $start_day[1], $start_day[0], $start_day[2]);
    if ($repeats['repeats'] == 0) {
        $new_time += 86400;
    } else {
        if ($repeats['repeats'] == 1) {
            // If this isn't the first time we've called this function then we can just tack on the time since $last_occurance
            if ($first == false) {
                $new_time += 86400 * $repeats['days'];
            } else {
                // Need to count it out
                if ($range['start'] > $event['starttime']) {
                    $days_since = ceil(($range['start'] - $start_date) / 86400);
                    $occurances = floor($days_since / $repeats['days']);
                    $next_date = $occurances * $repeats['days'];
                    $new_time = $event['starttime'] + 86400 * $next_date;
                } else {
                    $new_time = $start_date;
                }
            }
        } else {
            if ($repeats['repeats'] == 2) {
                if ($first == false) {
                    $last_dow = gmdate("w", $last_occurance);
                    // Last day of week = friday, +3 gives monday
                    if ($last_dow == 5) {
                        $new_time += 86400 * 3;
                    } else {
                        $new_time += 86400;
                    }
                } else {
                    if ($range['start'] < $event['starttime']) {
                        $start = $event['starttime'];
                    } else {
                        $start = $range['start'];
                    }
                    $first_dow = gmdate("w", $start);
                    if ($first_dow == 6) {
                        $new_time = $start + 86400 * 2;
                    } else {
                        if ($first_dow == 0) {
                            $new_time = $start + 86400;
                        } else {
                            $new_time = $start;
                        }
                    }
                }
            } else {
                if ($repeats['repeats'] == 3) {
                    $weekdays = fetch_weekday_structure($event['weekday_start']);
                    $last_dow = gmdate("w", $last_occurance);
                    if ($first == true) {
                        $last_dow = -1;
                        $start_day = gmdate('w', $last_occurance);
                        if (in_array($start_day, $weekdays)) {
                            $next_dow = 0;
                        }
                    } else {
                        foreach ($repeats['days'] as $weekday) {
                            if ($weekday > $last_dow) {
                                $next_dow = $weekday;
                                break;
                            }
                        }
                    }
                    if (!isset($next_dow)) {
                        // Fetch first weekday
                        $first = $repeats['days'][0] * 86400;
                        $new_time += $first;
                        // Increase x weeks
                        $new_time += (7 - $last_dow) * 86400;
                        $new_time += ($repeats['weeks'] - 1) * 604800;
                    } else {
                        // Next day of week exists
                        if ($last_dow > 0) {
                            $day_diff = $next_dow - $last_dow;
                        } else {
                            $day_diff = $next_dow;
                        }
                        $new_time += $day_diff * 86400;
                    }
                } else {
                    if ($repeats['repeats'] == 4) {
                        $last_month = gmdate("n", $last_occurance);
                        $last_year = gmdate("Y", $last_occurance);
                        $last_day = gmdate("j", $last_occurance);
                        $last_num_days = gmdate("t", $last_occurance);
                        // X of every Y months
                        if ($repeats['day']) {
                            if ($first == true) {
                                if ($last_day <= $repeats['day']) {
                                    $new_time = gmmktime(0, 0, 0, $last_month, $repeats['day'], $last_year);
                                } else {
                                    $new_time = gmmktime(0, 0, 0, $last_month + 1, $repeats['day'], $last_year);
                                    if ($new_time > $event['endtime']) {
                                        return false;
                                    }
                                }
                            } else {
                                $new_time = gmmktime(0, 0, 0, $last_month + $repeats['months'], $repeats['day'], $last_year);
                            }
                        } else {
                            if ($first == true) {
                                $new_time = fetch_weekday_monthly_repetition($repeats, $last_month, $last_year);
                                if ($new_time < $last_occurance) {
                                    $new_time = fetch_weekday_monthly_repetition($repeats, $last_month + 1, $last_year);
                                }
                            } else {
                                $new_time = fetch_weekday_monthly_repetition($repeats, $last_month + $repeats['months'], $last_year);
                            }
                        }
                    } else {
                        if ($repeats['repeats'] == 5) {
                            $last_year = gmdate("Y", $last_occurance);
                            // Repeats on (day) of (month) every (years)
                            if ($repeats['day']) {
                                if ($first == true) {
                                    $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year);
                                    if ($new_time < $last_occurance) {
                                        $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year + 1);
                                    }
                                } else {
                                    $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year + $repeats['years']);
                                }
                            } else {
                                if ($first == true) {
                                    $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year);
                                    if ($new_time < $last_occurance) {
                                        $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year + 1);
                                    }
                                } else {
                                    $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year + $repeats['years']);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $new_time;
}
コード例 #2
0
ファイル: calendar.php プロジェクト: GeorgeLVP/mybb
     $year = my_date("Y");
 }
 // Then the month
 if ($mybb->input['month'] >= 1 && $mybb->input['month'] <= 12) {
     $month = intval($mybb->input['month']);
 } else {
     $month = my_date("n");
 }
 add_breadcrumb(htmlspecialchars_uni($calendar['name']), get_calendar_link($calendar['cid']));
 add_breadcrumb("{$monthnames[$month]} {$year}", get_calendar_link($calendar['cid'], $year, $month));
 $next_month = get_next_month($month, $year);
 $prev_month = get_prev_month($month, $year);
 $prev_link = get_calendar_link($calendar['cid'], $prev_month['year'], $prev_month['month']);
 $next_link = get_calendar_link($calendar['cid'], $next_month['year'], $next_month['month']);
 // Start constructing the calendar
 $weekdays = fetch_weekday_structure($calendar['startofweek']);
 $month_start_weekday = gmdate("w", gmmktime(0, 0, 0, $month, $calendar['startofweek'] + 1, $year));
 $prev_month_days = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
 // This is if we have days in the previous month to show
 if ($month_start_weekday != $weekdays[0] || $calendar['startofweek'] != 0) {
     $prev_days = $day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
     $day -= array_search($month_start_weekday, $weekdays);
     $day += $calendar['startofweek'] + 1;
     if ($day > $prev_month_days + 1) {
         // Go one week back
         $day -= 7;
     }
     $calendar_month = $prev_month['month'];
     $calendar_year = $prev_month['year'];
 } else {
     $day = $calendar['startofweek'] + 1;