You are here

protected static function WebformDateHelper::convertDateStringToEnglish in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Utility/WebformDateHelper.php \Drupal\webform\Utility\WebformDateHelper::convertDateStringToEnglish()

Convert date string to English so that it can be parsed.

Parameters

string $format: PHP date() type format for parsing the input.

string $value: A date string.

Return value

string A date string translated to English.

See also

https://stackoverflow.com/questions/36498186/php-datetimecreatefromforma...

core/modules/locale/locale.datepicker.js

1 call to WebformDateHelper::convertDateStringToEnglish()
WebformDateHelper::createFromFormat in src/Utility/WebformDateHelper.php
Creates a date object from an input format with a translated date string.

File

src/Utility/WebformDateHelper.php, line 239

Class

WebformDateHelper
Helper class webform date helper methods.

Namespace

Drupal\webform\Utility

Code

protected static function convertDateStringToEnglish($format, $value) {

  // Do not convert English date strings.
  if (\Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId() === 'en') {
    return $value;
  }

  // F = A full textual representation of a month, such as January or March.
  if (strpos($format, 'F') !== FALSE) {
    $month_names_untranslated = DateHelper::monthNamesUntranslated();
    $month_names_translated = DateHelper::monthNames();
    foreach ($month_names_untranslated as $index => $month_name_untranslated) {
      $value = str_ireplace((string) $month_names_translated[$index], $month_name_untranslated, $value);
    }
  }

  // M =	A short textual representation of a month, three letters.
  if (strpos($format, 'M') !== FALSE) {
    $month_names_abbr_untranslated = DateHelper::monthNamesAbbrUntranslated();
    $month_names_abbr_translated = DateHelper::monthNamesAbbr();
    foreach ($month_names_abbr_untranslated as $index => $month_name_abbr_untranslated) {
      $value = str_ireplace((string) $month_names_abbr_translated[$index], $month_name_abbr_untranslated, $value);
    }
  }

  // l = A full textual representation of the day of the week.
  if (strpos($format, 'l') !== FALSE) {
    $week_days_untranslated = DateHelper::weekDaysUntranslated();
    $week_days_translated = DateHelper::weekDays();
    foreach ($week_days_untranslated as $index => $week_day_untranslated) {
      $value = str_ireplace((string) $week_days_translated[$index], $week_day_untranslated, $value);
    }
  }

  // D = A textual representation of a day, three letters.
  if (strpos($format, 'D') !== FALSE) {
    $week_days_abbr_untranslated = DateHelper::weekDaysUntranslated();
    $week_days_abbr_translated = DateHelper::weekDaysAbbr();
    foreach ($week_days_abbr_untranslated as $index => $week_day_abbr_untranslated) {
      $week_days_abbr_untranslated[$index] = (string) substr($week_days_abbr_untranslated[$index], 0, 3);
      $value = str_ireplace((string) $week_days_abbr_translated[$index], $week_days_abbr_untranslated[$index], $value);
    }
  }
  return $value;
}