You are here

public function FeedsDateTime::__construct in Feeds 8.2

Overridden constructor.

Parameters

$time: time string, flexible format including timestamp. Invalid formats will fall back to 'now'.

$tz: PHP \DateTimeZone object, NULL allowed

1 call to FeedsDateTime::__construct()
FeedsDateTime::__wakeup in lib/Drupal/feeds/FeedsDateTime.php
Upon unserializing, we must re-build ourselves using local variables.

File

lib/Drupal/feeds/FeedsDateTime.php, line 51

Class

FeedsDateTime
Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.

Namespace

Drupal\feeds

Code

public function __construct($time = '', $tz = NULL) {

  // Assume UNIX timestamp if numeric.
  if (is_numeric($time)) {

    // Make sure it's not a simple year
    if (is_string($time) && strlen($time) > 4 || is_int($time)) {
      $time = "@" . $time;
    }
  }

  // PHP < 5.3 doesn't like the GMT- notation for parsing timezones.
  $time = str_replace("GMT-", "-", $time);
  $time = str_replace("GMT+", "+", $time);

  // Some PHP 5.2 version's DateTime class chokes on invalid dates.
  if (!strtotime($time)) {
    $time = 'now';
  }

  // Create and set time zone separately, PHP 5.2.6 does not respect time zone
  // argument in __construct().
  parent::__construct($time);
  $tz = $tz ? $tz : new \DateTimeZone("UTC");
  $this
    ->setTimeZone($tz);

  // Verify that timezone has not been specified as an offset.
  if (!preg_match('/[a-zA-Z]/', $this
    ->getTimezone()
    ->getName())) {
    $this
      ->setTimezone(new \DateTimeZone("UTC"));
  }

  // Finally set granularity.
  $this
    ->setGranularityFromTime($time, $tz);
}