You are here

public function DateObject::__construct in Date 7.2

Same name and namespace in other branches
  1. 7.3 date_api/date_api.module \DateObject::__construct()
  2. 7 date_api/date_api.module \DateObject::__construct()

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()
DateObject::__wakeup in date_api/date_api.module
Re-builds the object using local variables.

File

date_api/date_api.module, line 220
This module will make the date API available to other modules.

Class

DateObject
Extend PHP DateTime class.

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);
  }
  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_array($time) && preg_match('`^-?\\d+$`', $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 we haven't got a valid timezone name yet, we need to set one or
  // we will get undefined index errors.
  // This can happen if $time had an offset or no timezone.
  if (!$this
    ->getTimezone() || !preg_match('/[a-zA-Z]/', $this
    ->getTimezone()
    ->getName())) {

    // If the original $tz has a name, use it.
    if (preg_match('/[a-zA-Z]/', $tz
      ->getName())) {
      $this
        ->setTimezone($tz);
    }
    else {
      $this
        ->setTimezone(new DateTimeZone("UTC"));
      $this->errors['timezone'] = t('No valid timezone name was provided.');
    }
  }
}