You are here

public function DateList::getDateArray in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 src/Plugin/conditional_fields/handler/DateList.php \Drupal\conditional_fields\Plugin\conditional_fields\handler\DateList::getDateArray()

Get the dateTime object form string.

Parameters

string $value: The value to parse as a DrupalDateTime.

Return value

\Drupal\Core\Datetime\DrupalDateTime The parsed DrupalDateTime.

1 call to DateList::getDateArray()
DateList::statesHandler in src/Plugin/conditional_fields/handler/DateList.php
Executes states handler according to conditional fields settings.

File

src/Plugin/conditional_fields/handler/DateList.php, line 116

Class

DateList
Provides states handler for date lists.

Namespace

Drupal\conditional_fields\Plugin\conditional_fields\handler

Code

public function getDateArray($value) {
  $date = new DrupalDateTime($value);
  if ($date
    ->hasErrors()) {
    $pattern = "/\\\\?[\\/]+|[-\\.\\,\\s]+/";
    $parts = preg_split($pattern, $value);
    $data_data = [
      'year' => isset($parts[0]) ? (int) $parts[0] : 1900,
      'month' => isset($parts[1]) ? (int) $parts[1] : 1,
      'day' => isset($parts[2]) ? (int) $parts[2] : 1,
      'hour' => isset($parts[3]) ? (int) $parts[3] : 0,
      'minute' => isset($parts[4]) ? (int) $parts[4] : 0,
      'second' => isset($parts[5]) ? (int) $parts[5] : 0,
    ];
  }
  else {
    $data_data = [
      'year' => (int) $date
        ->format("Y"),
      'month' => (int) $date
        ->format("n"),
      'day' => (int) $date
        ->format("j"),
      'hour' => (int) $date
        ->format("G"),
      'minute' => (int) $date
        ->format("i"),
      'second' => (int) $date
        ->format("s"),
    ];
  }
  return $data_data;
}