You are here

public static function PersianDrupalDateTime::createFromFormat in Persian Date for Drupal 8 8.4

Same name and namespace in other branches
  1. 8 src/Plugin/Datetime/PersianDrupalDateTime.php \Drupal\persian_date\Plugin\Datetime\PersianDrupalDateTime::createFromFormat()

Creates a date object from an input format.

Parameters

string $format: PHP date() type format for parsing the input. This is recommended to use things like negative years, which php's parser fails on, or any other specialized input with a known format. If provided the date will be created using the createFromFormat() method. @see http://php.net/manual/datetime.createfromformat.php

string $time: String representing the time.

mixed $timezone: (optional) \DateTimeZone object, time zone string or NULL. See __construct() for more details.

array $settings: (optional) A keyed array for settings, suitable for passing on to __construct(). Supports an additional key:

  • validate_format: (optional) Boolean choice to validate the created date using the input format. The format used in createFromFormat() allows slightly different values than format(). Using an input format that works in both functions makes it possible to a validation step to confirm that the date created from a format string exactly matches the input. This option indicates the format can be used for validation. Defaults to TRUE.

Return value

static A new DateTimePlus object.

Throws

\InvalidArgumentException If the a date cannot be created from the given format.

\UnexpectedValueException If the created date does not match the input value.

Overrides DateTimePlus::createFromFormat

2 calls to PersianDrupalDateTime::createFromFormat()
PersianDateList::valueCallback in src/Element/PersianDateList.php
Validates the date type to adjust 12 hour time and prevent invalid dates. If the date is valid, the date is set in the form.
PersianDateTime::valueCallback in src/Element/PersianDateTime.php
Determines how user input is mapped to an element's #value property.

File

src/Plugin/Datetime/PersianDrupalDateTime.php, line 42

Class

PersianDrupalDateTime

Namespace

Drupal\persian_date\Plugin\Datetime

Code

public static function createFromFormat($format, $time, $timezone = NULL, $settings = []) {
  if (!isset($settings['validate_format'])) {
    $settings['validate_format'] = TRUE;
  }

  // Tries to create a date from the format and use it if possible.
  // A regular try/catch won't work right here, if the value is
  // invalid it doesn't return an exception.
  $datetimeplus = new static('', $timezone, $settings);
  $date = false;
  if ($time) {
    list($year, $month, $day, $hour, $minute, $second) = array_values(jDateTime::parseFromFormat($format, $time));
    $date = PersianDateFactory::buildFromExactDate((int) $hour, (int) $minute, (int) $second, (int) $month, (int) $day, (int) $year);
  }
  if (!$date instanceof \DateTime) {
    throw new \InvalidArgumentException('The date cannot be created from a format.');
  }
  else {

    // Functions that parse date is forgiving, it might create a date that
    // is not exactly a match for the provided value, so test for that by
    // re-creating the date/time formatted string and comparing it to the input. For
    // instance, an input value of '11' using a format of Y (4 digits) gets
    // created as '0011' instead of '2011'.
    if ($date instanceof DateTimePlus) {
      $test_time = $date
        ->format($format, $settings);
    }
    elseif ($date instanceof \DateTime) {
      $test_time = $date
        ->format($format);
    }
    if ($settings['validate_format'] && $test_time != $time) {
      throw new \UnexpectedValueException('The created date does not match the input value.');
    }
    $date = $date
      ->getOriginalDateTime();
    $date
      ->setTimezone($datetimeplus
      ->getTimezone());
    $datetimeplus
      ->setTimestamp($date
      ->getTimestamp());
    $datetimeplus
      ->setTimezone($date
      ->getTimezone());
  }
  return $datetimeplus;
}