You are here

function date_repeat_set_year_day in Date 7.2

Same name and namespace in other branches
  1. 5.2 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  2. 6.2 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  3. 6 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  4. 7.3 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  5. 7 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()

Set a date object to a specific day of the year.

Example, date_set_year_day($date, 'Sunday', 2, '-') will reset $date to the second to last Sunday in the year. If $day is empty, will set to the number of days from the beginning or end of the year.

1 call to date_repeat_set_year_day()
_date_repeat_calc in date_repeat/date_repeat_calc.inc
Private implementation of date_repeat_calc().

File

date_repeat/date_repeat_calc.inc, line 568
Code to compute the dates that match an iCal RRULE.

Code

function date_repeat_set_year_day($date_in, $month, $day, $count = 1, $direction = '+', $timezone = 'UTC', $modify_time = '') {
  if (is_object($date_in)) {
    $current_year = date_format($date_in, 'Y');

    // Reset to the start of the month. See note above.
    $datetime = date_format($date_in, DATE_FORMAT_DATETIME);
    $month_key = isset($month) ? $month : '01';
    $datetime = substr_replace($datetime, $month_key . '-01', 5, 5);
    $date = new DateObject($datetime, $timezone);
    if (isset($month)) {
      if ($direction == '-') {

        // For negative search, start from the end of the month.
        $modifier = '+1 month';
      }
      else {

        // For positive search, back up one day to get outside the current
        // month, so we can catch the first of the month.
        $modifier = '-1 day';
      }
    }
    else {
      if ($direction == '-') {

        // For negative search, start from the end of the year.
        $modifier = '+1 year';
      }
      else {

        // For positive search, back up one day to get outside the current
        // year, so we can catch the first of the year.
        $modifier = '-1 day';
      }
    }
    date_modify($date, $modifier . $modify_time);
    if (empty($day)) {
      date_modify($date, $direction . $count . ' days' . $modify_time);
    }
    else {

      // Use the English text for order, like First Sunday instead of +1 Sunday
      // to overcome PHP5 bug, (see #369020).
      $order = date_order();
      $step = $count <= 5 ? $order[$direction . $count] : $count;
      date_modify($date, $step . ' ' . $day . $modify_time);
    }

    // If that takes us outside the current year, don't go there.
    if (date_format($date, 'Y') == $current_year) {
      return $date;
    }
  }
  return $date_in;
}