You are here

function date_repeat_set_year_day in Date 5.2

Same name and namespace in other branches
  1. 6.2 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  2. 6 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  3. 7.3 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  4. 7 date_repeat/date_repeat_calc.inc \date_repeat_set_year_day()
  5. 7.2 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 520
Code to compute the dates that match an iCal RRULE.

Code

function date_repeat_set_year_day($date_in, $day, $count = 1, $direction = '+', $timezone = 'UTC') {
  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);
    $datetime = substr_replace($datetime, '01-01', 5, 5);
    $date = date_make_date($datetime, $timezone);
    if ($direction == '-') {

      // For negative search, start from the end of the year.
      date_modify($date, '+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.
      date_modify($date, '-1 day');
    }
    if (empty($day)) {
      $day = 'days';
    }
    if (empty($day)) {
      date_modify($date, $direction . $count . ' days');
    }
    else {

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

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