You are here

public function Date::__construct in Views TimelineJS integration 8.3

Constructs a new Date object.

@todo Change the exception to an InvalidArgumentException.

Parameters

string $date_string: A string representing a date.

\DateTimeZone|null $timezone: The date's timezone.

Throws

\Exception

File

src/TimelineJS/Date.php, line 33

Class

Date
Converts date strings to TimelineJS3-compatible date arrays.

Namespace

Drupal\views_timelinejs\TimelineJS

Code

public function __construct($date_string, DateTimeZone $timezone = NULL) {
  $this->dateString = $date_string;

  // Disallow empty date strings.  They will cause DateTime::__construct() to
  // return a date object with the current time.
  if (empty($date_string)) {
    throw new Exception('Empty date strings are not allowed.');
  }

  // Check for date strings that only include a year value.
  if (is_numeric($date_string)) {

    // Append '-01-01' to year-only values.  By specifying a month and day
    // before the value is parsed, year-only values can be used as input.
    $date_string .= '-01-01';
  }
  parent::__construct($date_string, $timezone);
}