You are here

public static function OpignoDateRangeWidget::createDateTimeFromWrapper in Opigno calendar 8

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldWidget/OpignoDateRangeWidget.php \Drupal\opigno_calendar\Plugin\Field\FieldWidget\OpignoDateRangeWidget::createDateTimeFromWrapper()

Creates datetime from components.

Parameters

array $wrapper: Datetime field wrapper.

string $pattern: Date pattern.

Return value

\Drupal\Core\Datetime\DrupalDateTime Datetime object.

Throws

\Exception

1 call to OpignoDateRangeWidget::createDateTimeFromWrapper()
OpignoDateRangeWidget::massageFormValues in src/Plugin/Field/FieldWidget/OpignoDateRangeWidget.php
Massages the form values into the format expected for field values.

File

src/Plugin/Field/FieldWidget/OpignoDateRangeWidget.php, line 254

Class

OpignoDateRangeWidget
Plugin implementation of the 'opigno_daterange' widget.

Namespace

Drupal\opigno_calendar\Plugin\Field\FieldWidget

Code

public static function createDateTimeFromWrapper(array $wrapper, $pattern = NULL) {
  $display_format = !empty($pattern) ? "{$pattern} H:i:s" : 'm/d/Y H:i:s';
  $raw_date = $wrapper['date'];
  $raw_hours = $wrapper['hours'];
  $raw_minutes = $wrapper['minutes'];
  $date_str = "{$raw_date} 00:00:00";
  $time_str = "PT{$raw_hours}H{$raw_minutes}M";
  $date = DrupalDateTime::createFromFormat($display_format, $date_str);
  $date
    ->add(new \DateInterval($time_str));
  return $date;
}