DateObject::__construct

7 date_api.module public DateObject::__construct($time = 'now', $tz = NULL, $format = NULL)

Constructs a date object.

Parameters

string $time: A date/time string or array. Defaults to 'now'.

object|string|null $tz: PHP DateTimeZone object, string or NULL allowed. Defaults to NULL.

string $format: PHP date() type format for parsing. Doesn't support timezones; if you have a timezone, send NULL and the default constructor method will hopefully parse it. $format is recommended in order to use negative or large years, which php's parser fails on.

1 call to DateObject::__construct()

File

contributions/date/date_api/date_api.module, line 197
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Code

public function __construct($time = 'now', $tz = NULL, $format = NULL) {
  $this->timeOnly = FALSE;
  $this->dateOnly = FALSE;

  // Store the raw time input so it is available for validation.
  $this->originalTime = $time;

  // Allow string timezones.
  if (!empty($tz) && !is_object($tz)) {
    $tz = new DateTimeZone($tz);
  }

  // Default to the site timezone when not explicitly provided.
  elseif (empty($tz)) {
    $tz = date_default_timezone_object();
  }
  // Special handling for Unix timestamps expressed in the local timezone.
  // Create a date object in UTC and convert it to the local timezone. Don't
  // try to turn things like '2010' with a format of 'Y' into a timestamp.
  if (is_numeric($time) && (empty($format) || $format == 'U')) {
    // Assume timestamp.
    $time = "@" . $time;
    $date = new DateObject($time, 'UTC');
    if ($tz->getName() != 'UTC') {
      $date->setTimezone($tz);
    }
    $time = $date->format(DATE_FORMAT_DATETIME);
    $format = DATE_FORMAT_DATETIME;
    $this->addGranularity('timezone');
  }
  elseif (is_array($time)) {
    // Assume we were passed an indexed array.
    if (empty($time['year']) && empty($time['month']) && empty($time['day'])) {
      $this->timeOnly = TRUE;
    }
    if (empty($time['hour']) && empty($time['minute']) && empty($time['second'])) {
      $this->dateOnly = TRUE;
    }
    $this->errors = $this->arrayErrors($time);
    // Make this into an ISO date, forcing a full ISO date even if some values
    // are missing.
    $time = $this->toISO($time, TRUE);
    // We checked for errors already, skip parsing the input values.
    $format = NULL;
  }
  else {
    // Make sure dates like 2010-00-00T00:00:00 get converted to
    // 2010-01-01T00:00:00 before creating a date object
    // to avoid unintended changes in the month or day.
    $time = date_make_iso_valid($time);
  }

  // The parse function will also set errors on the date parts.
  if (!empty($format)) {
    $arg = self::$allgranularity;
    $element = array_pop($arg);
    while (!$this->parse($time, $tz, $format) && $element != 'year') {
      $element = array_pop($arg);
      $format = date_limit_format($format, $arg);
    }
    if ($element == 'year') {
      return FALSE;
    }
  }
  elseif (is_string($time)) {
    // PHP < 5.3 doesn't like the GMT- notation for parsing timezones.
    $time = str_replace("GMT-", "-", $time);
    $time = str_replace("GMT+", "+", $time);
    // We are going to let the parent dateObject do a best effort attempt to
    // turn this string into a valid date. It might fail and we want to
    // control the error messages.
    try {
      @parent::__construct($time, $tz);
    }
    catch (Exception $e) {
      $this->errors['date'] = $e;
      return;
    }
    if (empty($this->granularity)) {
      $this->setGranularityFromTime($time, $tz);
    }
  }
  // If this tz was given as just an offset or the timezone
  // was invalid, we need to do some tweaking.
  if (!$this->getTimezone() || !preg_match('/[a-zA-Z]/', $this->getTimezone()->getName())) {

    // If the timezone name is an offset and the original
    // $tz has a name, use it. This happens if you pass in
    // a date string with an offset along with a specific timezone name.
    if (!preg_match('/[a-zA-Z]/', $this->getTimezone()->getName()) && preg_match('/[a-zA-Z]/', $tz->getName())) {
      $this->setTimezone($tz);
    }
    // If we get this far, we have no information about the timezone name,
    // but we will get undefined index errors without any name.
    else {
      $this->setTimezone(new DateTimeZone("UTC"));
      $this->errors['timezone'] = t('No valid timezone name was provided.');
    }
  }
}