You are here

public static function BirthdaysBirthday::fromDate in Birthdays 7

Creates a birthday from values.

Parameters

$year: The year or empty for no year.

$month: The month (1 to 12).

$day: The day (1 to 31).

$allow_future: Allow values in the futzre. Default is FALSE.

Throws

InvalidArgumentException when one of the given values is invalid.

4 calls to BirthdaysBirthday::fromDate()
BirthdaysBirthday::fromArray in ./birthdays_birthday.inc
Creates a birthday from a database value.
BirthdaysBirthday::fromOffset in ./birthdays_birthday.inc
Creates a BirthdaysBirthday instance from an offset of dates from the current time. That does not include a year.
BirthdaysBirthdayTestCase::testFromDate in ./birthdays.test
Tests conversion directily given values.
birthdays_lookup in ./birthdays.module
Page callback: Evaluates a date format for live display.

File

./birthdays_birthday.inc, line 49
The BirthdaysBirthday class.

Class

BirthdaysBirthday
Converts between different data representations and do calculations on a birthday.

Code

public static function fromDate($year, $month, $day, $allow_future = FALSE) {

  // All parameters are integers.
  $year = intval($year);
  $month = intval($month);
  $day = intval($day);

  // Check for all empty.
  if (!$year && !$month && !$day) {
    return self::fromEmpty();
  }

  // Validate month.
  if ($month < 1 || $month > 12) {
    throw new InvalidArgumentException(t("%month is not a valid month.", array(
      '%month' => $month,
    )));
  }

  // The maximum days of a month, in a leap year. Indexed by month.
  $maxdays = array(
    1 => 31,
    29,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31,
  );
  if ($year) {

    // Negative years not allowed.
    if ($year < 0) {
      throw new InvalidArgumentException(t('Negative years (%year) are not allowed.', array(
        '%year' => $year,
      )));
    }

    // Adjust year if only 2 digits are given.
    if ($year <= 99) {
      if ($year >= date('y')) {
        $year += date('Y') - date('y') - 100;
      }
      else {
        $year += date('Y') - date('y');
      }
    }

    // Check if it's in the past more than the world record.
    if ($year < date('Y') - 115) {
      throw new InvalidArgumentException(t("%year is too far in the past.", array(
        '%year' => $year,
      )));
    }

    // Februrary has only 28 days if it's not a leap year.
    if (!($year % 400 == 0 || $year % 4 == 0 && $year % 100 != 0)) {
      $maxdays[2] = 28;
    }

    // Check if it's in the future.
    if (!$allow_future) {
      $unixtime = mktime(1, 0, 0, $month, $day, $year);
      if ($unixtime >= REQUEST_TIME) {
        throw new InvalidArgumentException(t("The given date is in the future."));
      }
    }
  }

  // Validate day.
  if ($day < 1 || $day > $maxdays[$month]) {
    throw new InvalidArgumentException(t("%day is not a valid day of month %month", array(
      '%day' => $day,
      '%month' => $month,
    )));
  }
  return new BirthdaysBirthday(array(
    'year' => $year,
    'month' => $month,
    'day' => $day,
  ));
}